bool script_state_load_single(int script_idx,bool checkpoint,char *err_str) { int prop_name_len,prop_value_len; char prop_name[256]; char *prop_value; script_type *script; JSStringRef js_prop_name,js_prop_json; JSValueRef js_prop_value; if (script_idx==-1) return(TRUE); // get the script script=js.script_list.scripts[script_idx]; // run through the properties to replace // a 0 property name length means the end // of properties for this script while (TRUE) { // get the prop name and JSON value game_file_get_chunk(&prop_name_len); if (prop_name_len==0) break; game_file_get_chunk(prop_name); game_file_get_chunk(&prop_value_len); prop_value=(char*)malloc(prop_value_len); if (prop_value==NULL) { strcpy(err_str,"Out of Memory"); return(FALSE); } game_file_get_chunk(prop_value); // process the property js_prop_name=JSStringCreateWithUTF8CString(prop_name); js_prop_json=JSStringCreateWithUTF8CString(prop_value); js_prop_value=JSValueMakeFromJSONString(script->cx,js_prop_json); JSStringRelease(js_prop_json); JSObjectSetProperty(script->cx,script->global_obj,js_prop_name,js_prop_value,0,NULL); JSStringRelease(js_prop_name); } // send load event to script scripts_post_event(script_idx,-1,sd_event_state,(checkpoint?sd_event_state_load_checkpoint:sd_event_state_load),0,err_str); return(TRUE); }
bool game_file_load(char *file_name,char *err_str) { char *c,path[1024],fname[256]; file_save_header head; // load and expand strcpy(fname,file_name); c=strrchr(fname,'.'); if (c!=NULL) *c=0x0; // remove any extensions file_paths_documents(&setup.file_path_setup,path,"Saved Games",fname,"sav"); if (!game_file_expand_load(path,err_str)) return(FALSE); game_file_pos=0; progress_initialize("Loading"); // if game isn't running, then start if (!server.game_open) { if (!game_start(skill_medium,NULL,err_str)) { free(game_file_data); return(FALSE); } } // get header game_file_get_chunk(&head); // check version if (strcmp(head.version,dim3_version)!=0) { sprintf(err_str,"This saved game file is from a different version of dim3"); free(game_file_data); return(FALSE); } // reload map progress_draw(10); if ((!server.map_open) || (strcmp(head.map_name,map.info.name)!=0)) { // need to load a map? if (server.map_open) map_end(); strcpy(map.info.name,head.map_name); map.info.player_start_name[0]=0x0; map.info.player_start_type[0]=0x0; map.info.in_load=TRUE; if (!map_start(TRUE,err_str)) { free(game_file_data); return(FALSE); } } // timing game_time_set(head.tick); // view and server objects progress_draw(20); game_file_get_chunk(&view.time); game_file_get_chunk(&view.fps); game_file_get_chunk(&camera); game_file_get_chunk(&server.time); game_file_get_chunk(&server.player_obj_uid); game_file_get_chunk(&server.skill); game_file_get_chunk(&server.uid); game_file_get_chunk(&server.count); progress_draw(30); free(server.objs); free(server.weapons); free(server.proj_setups); server.objs=(obj_type*)game_file_replace_chunk(); server.weapons=(weapon_type*)game_file_replace_chunk(); server.proj_setups=(proj_setup_type*)game_file_replace_chunk(); if ((server.objs==NULL) || (server.weapons==NULL) || (server.proj_setups==NULL)) { free(game_file_data); return(FALSE); } progress_draw(40); game_file_get_chunk(server.projs); game_file_get_chunk(server.effects); game_file_get_chunk(server.decals); progress_draw(50); game_file_get_chunk(hud.bitmaps); game_file_get_chunk(hud.texts); game_file_get_chunk(hud.bars); game_file_get_chunk(&hud.radar); // map changes progress_draw(60); game_file_get_chunk(&map.ambient); game_file_get_chunk(&map.rain); game_file_get_chunk(&map.background); game_file_get_chunk(&map.sky); game_file_get_chunk(&map.fog); progress_draw(70); map_group_dispose_unit_list(&map); // need to destroy and rebuild unit lists game_file_get_chunk(map.groups); map_group_create_unit_list(&map); game_file_get_chunk(map.movements); group_moves_synch_with_load(); // script objects progress_draw(80); game_file_get_chunk(&js.script_current_uid); game_file_get_chunk(&js.count); game_file_get_chunk(&js.time); game_file_get_chunk(js.timers); game_file_get_chunk(js.globals); // reset model UIDs progress_draw(90); models_reset_uid(); // send scripts load event // to restore globals progress_draw(95); script_state_load(); // free game data progress_draw(100); free(game_file_data); // finished progress_shutdown(); // fix some necessary functions map.rain.reset=TRUE; fade_screen_cancel(); // return to old game time game_time_reset(); return(TRUE); }