/* * =============== * PR_LoadProgs * =============== */ public static void PR_LoadProgs() { int i; byte[] buf; int kk; helper.ByteBuffer bbuf; crc.CRC_Init(ref pr_crc); buf = common.COM_LoadHunkFile("progs.dat"); progs = (dprograms_t)buf; if (progs == null) { sys_linux.Sys_Error("PR_LoadProgs: couldn't load progs.dat"); } console.Con_DPrintf("Programs occupy " + (common.com_filesize / 1024) + "K.\n"); for (i = 0; i < common.com_filesize; i++) { crc.CRC_ProcessByte(ref pr_crc, buf[i]); } if (progs.version != PROG_VERSION) { sys_linux.Sys_Error("progs.dat has wrong version number (" + progs.version + " should be " + PROG_VERSION + ")"); } if (progs.crc != PROGHEADER_CRC) { sys_linux.Sys_Error("progs.dat system vars have been modified, progdefs.h is out of date"); } bbuf = new helper.ByteBuffer(buf); //pr_functions = (dfunction_t*)((byte*)progs + progs.ofs_functions); bbuf.ofs = progs.ofs_functions; pr_functions = new dfunction_t[progs.numfunctions]; for (kk = 0; kk < progs.numfunctions; kk++) { pr_functions[kk] = (dfunction_t)bbuf; bbuf.ofs += sizeof_dfunction_t; } //pr_strings = (char*)progs + progs.ofs_strings; pr_strings = buf; //pr_globaldefs = (ddef_t*)((byte*)progs + progs.ofs_globaldefs); bbuf.ofs = progs.ofs_globaldefs; pr_globaldefs = new ddef_t[progs.numglobaldefs]; for (kk = 0; kk < progs.numglobaldefs; kk++) { pr_globaldefs[kk] = (ddef_t)bbuf; bbuf.ofs += sizeof_ddef_t; } //pr_fielddefs = (ddef_t*)((byte*)progs + progs.ofs_fielddefs); bbuf.ofs = progs.ofs_fielddefs; pr_fielddefs = new ddef_t[progs.numfielddefs]; for (kk = 0; kk < progs.numfielddefs; kk++) { pr_fielddefs[kk] = (ddef_t)bbuf; bbuf.ofs += sizeof_ddef_t; } //pr_statements = (dstatement_t*)((byte*)progs + progs.ofs_statements); bbuf.ofs = progs.ofs_statements; pr_statements = new dstatement_t[progs.numstatements]; for (kk = 0; kk < progs.numstatements; kk++) { pr_statements[kk] = (dstatement_t)bbuf; bbuf.ofs += sizeof_dstatement_t; } //pr_global_struct = (globalvars_t*)((byte*)progs + progs.ofs_globals); bbuf.ofs = progs.ofs_globals; pr_global_struct = new globalvars_t[progs.numglobals * 4 / 368]; for (kk = 0; kk < progs.numglobals * 4 / 368; kk++) { pr_global_struct[kk] = (globalvars_t)bbuf; bbuf.ofs += sizeof_globalvars_t; } pr_edict_size = progs.entityfields * 4 + sizeof_edict_t - sizeof_entvars_t; }
public static void PR_LoadProgs() { FreeHandles(); ClearState(); _DynamicStrings.Clear(); // flush the non-C variable lookup cache for (int i = 0; i < q_shared.GEFV_CACHESIZE; i++) { gefvCache[i].field = null; } CRC_Init(out pr_crc); byte[] buf = COM_LoadFile("progs.dat"); progs = BytesToStructure <dprograms_t>(buf, 0); if (progs == null) { Sys_Error("PR_LoadProgs: couldn't load progs.dat"); } Con_DPrintf("Programs occupy {0}K.\n", buf.Length / 1024); for (int i = 0; i < buf.Length; i++) { CRC_ProcessByte(ref pr_crc, buf[i]); } // byte swap the header progs.SwapBytes(); if (progs.version != q_shared.PROG_VERSION) { Sys_Error("progs.dat has wrong version number ({0} should be {1})", progs.version, q_shared.PROG_VERSION); } if (progs.crc != q_shared.PROGHEADER_CRC) { Sys_Error("progs.dat system vars have been modified, progdefs.h is out of date"); } // Functions pr_functions = new dfunction_t[progs.numfunctions]; int offset = progs.ofs_functions; for (int i = 0; i < pr_functions.Length; i++, offset += dfunction_t.SizeInBytes) { pr_functions[i] = BytesToStructure <dfunction_t>(buf, offset); pr_functions[i].SwapBytes(); } // strings offset = progs.ofs_strings; int str0 = offset; for (int i = 0; i < progs.numstrings; i++, offset++) { // count string length while (buf[offset] != 0) { offset++; } } int length = offset - str0; pr_strings = Encoding.ASCII.GetString(buf, str0, length); // Globaldefs pr_globaldefs = new ddef_t[progs.numglobaldefs]; offset = progs.ofs_globaldefs; for (int i = 0; i < pr_globaldefs.Length; i++, offset += ddef_t.SizeInBytes) { pr_globaldefs[i] = BytesToStructure <ddef_t>(buf, offset); pr_globaldefs[i].SwapBytes(); } // Fielddefs pr_fielddefs = new ddef_t[progs.numfielddefs]; offset = progs.ofs_fielddefs; for (int i = 0; i < pr_fielddefs.Length; i++, offset += ddef_t.SizeInBytes) { pr_fielddefs[i] = BytesToStructure <ddef_t>(buf, offset); pr_fielddefs[i].SwapBytes(); if ((pr_fielddefs[i].type & q_shared.DEF_SAVEGLOBAL) != 0) { Sys_Error("PR_LoadProgs: pr_fielddefs[i].type & q_shared.DEF_SAVEGLOBAL"); } } // Statements pr_statements = new dstatement_t[progs.numstatements]; offset = progs.ofs_statements; for (int i = 0; i < pr_statements.Length; i++, offset += dstatement_t.SizeInBytes) { pr_statements[i] = BytesToStructure <dstatement_t>(buf, offset); pr_statements[i].SwapBytes(); } // Swap bytes inplace if needed if (!BitConverter.IsLittleEndian) { offset = progs.ofs_globals; for (int i = 0; i < progs.numglobals; i++, offset += 4) { SwapHelper.Swap4b(buf, offset); } } pr_global_struct = BytesToStructure <globalvars_t>(buf, progs.ofs_globals); _Globals = new float[progs.numglobals - globalvars_t.SizeInBytes / 4]; Buffer.BlockCopy(buf, progs.ofs_globals + globalvars_t.SizeInBytes, _Globals, 0, _Globals.Length * 4); pr_edict_size = progs.entityfields * 4 + dedict_t.SizeInBytes - entvars_t.SizeInBytes; _HGlobals = GCHandle.Alloc(_Globals, GCHandleType.Pinned); _GlobalsAddr = _HGlobals.AddrOfPinnedObject().ToInt64(); _HGlobalStruct = GCHandle.Alloc(pr_global_struct, GCHandleType.Pinned); _GlobalStructAddr = _HGlobalStruct.AddrOfPinnedObject().ToInt64(); }
/* =============== PR_LoadProgs =============== */ public static void PR_LoadProgs() { int i; Uint8Array buf; int kk; helper.ByteBuffer bbuf; pr_crc = crc.CRC_Init(); buf = common.COM_LoadHunkFile("progs.dat"); progs = (dprograms_t)buf; if (progs == null) sys_linux.Sys_Error("PR_LoadProgs: couldn't load progs.dat"); console.Con_DPrintf("Programs occupy " + (common.com_filesize / 1024) + "K.\n"); for (i = 0; i < common.com_filesize; i++) pr_crc = crc.CRC_ProcessByte(pr_crc, (byte)buf[i]); if (progs.version != PROG_VERSION) sys_linux.Sys_Error("progs.dat has wrong version number (" + progs.version + " should be " + PROG_VERSION + ")"); if (progs.crc != PROGHEADER_CRC) sys_linux.Sys_Error("progs.dat system vars have been modified, progdefs.h is out of date"); bbuf = new helper.ByteBuffer(buf); //pr_functions = (dfunction_t*)((byte*)progs + progs.ofs_functions); bbuf.ofs = progs.ofs_functions; pr_functions = new dfunction_t[progs.numfunctions]; for (kk = 0; kk < progs.numfunctions; kk++) { pr_functions[kk] = (dfunction_t)bbuf; bbuf.ofs += sizeof_dfunction_t; } //pr_strings = (char*)progs + progs.ofs_strings; pr_strings = buf; //pr_globaldefs = (ddef_t*)((byte*)progs + progs.ofs_globaldefs); bbuf.ofs = progs.ofs_globaldefs; pr_globaldefs = new ddef_t[progs.numglobaldefs]; for (kk = 0; kk < progs.numglobaldefs; kk++) { pr_globaldefs[kk] = (ddef_t)bbuf; bbuf.ofs += sizeof_ddef_t; } //pr_fielddefs = (ddef_t*)((byte*)progs + progs.ofs_fielddefs); bbuf.ofs = progs.ofs_fielddefs; pr_fielddefs = new ddef_t[progs.numfielddefs]; for (kk = 0; kk < progs.numfielddefs; kk++) { pr_fielddefs[kk] = (ddef_t)bbuf; bbuf.ofs += sizeof_ddef_t; } //pr_statements = (dstatement_t*)((byte*)progs + progs.ofs_statements); bbuf.ofs = progs.ofs_statements; pr_statements = new dstatement_t[progs.numstatements]; for (kk = 0; kk < progs.numstatements; kk++) { pr_statements[kk] = (dstatement_t)bbuf; bbuf.ofs += sizeof_dstatement_t; } //pr_global_struct = (globalvars_t*)((byte*)progs + progs.ofs_globals); bbuf.ofs = progs.ofs_globals; pr_global_struct = new globalvars_t[progs.numglobals * 4 / 368]; for (kk = 0; kk < progs.numglobals * 4 / 368; kk++) { pr_global_struct[kk] = (globalvars_t)bbuf; bbuf.ofs += sizeof_globalvars_t; } pr_edict_size = progs.entityfields * 4 + sizeof_edict_t - sizeof_entvars_t; }