Esempio n. 1
0
        public PythonModule(string name, IDictionary <object, object> dict, SystemState state, InitializeModule init, CallerContextFlags callerContextFlags)
        {
            Debug.Assert(state != null);

            if (dict is IAttributesDictionary)
            {
                __dict__ = (IAttributesDictionary)dict;
            }
            else
            {
                __dict__ = new FieldIdDict(dict);
            }
            ModuleName = name;
            __dict__[SymbolTable.Builtins] = TypeCache.Builtin;

            if (name == "__main__")
            {
                Debug.WriteLine("==========__main__");
            }

            initialize = init;

            contextFlags = callerContextFlags;
            systemState  = state;
        }
Esempio n. 2
0
        public void UpdateForReload(PythonModule reloaded)
        {
            this.__dict__   = reloaded.__dict__;
            this.initialize = reloaded.initialize;

            Initialize();
        }
Esempio n. 3
0
        private static object ImportBuiltin(PythonModule mod, string name)
        {
            mod.SystemState.TopPackage.Initialize(mod.SystemState);

            if (name.Equals("sys"))
            {
                return(mod.SystemState);
            }
            if (name.Equals("clr"))
            {
                ((ICallerContext)mod).ContextFlags |= CallerContextFlags.ShowCls;
                return(((ICallerContext)mod).SystemState.ClrModule);
            }
            Type ty;

            if (mod.SystemState.TopPackage.Builtins.TryGetValue(name, out ty))
            {
                if (typeof(CustomFieldIdDict).IsAssignableFrom(ty))
                {
                    CustomFieldIdDict dict = (CustomFieldIdDict)ty.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                    InitializeModule  init = (InitializeModule)Delegate.CreateDelegate(
                        typeof(InitializeModule), dict, "Initialize");
                    return(InitializeModule(name, new PythonModule(name, dict, mod.SystemState, init, CallerContextFlags.None)));
                }
                else
                {
                    return(MakePythonModule(mod, name, (ReflectedType)Ops.GetDynamicTypeFromType(ty)));
                }
            }
            return(null);
        }
        private static PythonModule DoGenerateModule(SystemState state, CompilerContext context, GlobalSuite gs, string moduleName, string sourceFileName, string outSuffix)
        {
            string fullPath;
            string outDir;
            string fileName;

            if (sourceFileName == "<stdin>")
            {
                fullPath = Environment.CurrentDirectory;
                outDir   = Environment.CurrentDirectory;
                fileName = "__stdin__";
            }
            else
            {
                fullPath = Path.GetFullPath(sourceFileName);
                outDir   = Options.BinariesDirectory == null?Path.GetDirectoryName(fullPath) : Options.BinariesDirectory;

                fileName = Path.GetFileNameWithoutExtension(sourceFileName);
            }

            AssemblyGen ag = new AssemblyGen(moduleName + outSuffix, outDir, fileName + outSuffix + ".exe", true);

            ag.SetPythonSourceFile(fullPath);


            TypeGen tg = GenerateModuleType(moduleName, ag);
            CodeGen cg = GenerateModuleInitialize(context, gs, tg);

            CodeGen main = GenerateModuleEntryPoint(tg, cg, moduleName, null);

            ag.SetEntryPoint(main.MethodInfo, PEFileKinds.ConsoleApplication);

            Type     ret  = tg.FinishType();
            Assembly assm = ag.DumpAndLoad();

            ret = assm.GetType(moduleName);

            CustomFieldIdDict dict = (CustomFieldIdDict)ret.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
            InitializeModule  init = (InitializeModule)Delegate.CreateDelegate(
                typeof(InitializeModule), dict, "Initialize");

            PythonModule pmod = new PythonModule(moduleName, dict, state, init);

            pmod.InitializeBuiltins();
            return(pmod);
        }
Esempio n. 5
0
        public static int ExecuteCompiled(CustomFieldIdDict compiled, InitializeModule init, string fullName, string[] references)
        {
            PythonEngine engine = new PythonEngine();

            engine.AddToPath(Environment.CurrentDirectory);

            engine.LoadAssembly(compiled.GetType().Assembly);
            PythonModule module     = new PythonModule(fullName, compiled, engine.engineContext.SystemState);
            string       executable = compiled.GetType().Assembly.Location;

            module.InitializeBuiltins();
            engine.Sys.prefix            = System.IO.Path.GetDirectoryName(executable);
            engine.Sys.executable        = executable;
            engine.Sys.exec_prefix       = engine.Sys.prefix;
            engine.Sys.modules[fullName] = module;

            if (references != null)
            {
                for (int i = 0; i < references.Length; i++)
                {
                    engine.engineContext.SystemState.ClrModule.AddReference(references[i]);
                }
            }

            // first arg is EXE
            List args = new List();

            string[] fullArgs = Environment.GetCommandLineArgs();
            args.Add(Path.GetFullPath(fullArgs[0]));
            for (int i = 1; i < fullArgs.Length; i++)
            {
                args.Add(fullArgs[i]);
            }
            engine.Sys.argv = args;

            try {
                init();
            } catch (PythonSystemExit x) {
                return(x.GetExitCode(engine.engineContext));
            } catch (Exception e) {
                engine.DumpException(e);
                return(-1);
            }
            return(0);
        }
Esempio n. 6
0
 public WardenMaiev()
 {
     WardenModule    = Array.Empty <byte>();
     ModuleName      = "";
     ModuleKey       = Array.Empty <byte>();
     ModuleData      = Array.Empty <byte>();
     ModuleSize      = 0;
     CheckIDs        = new byte[8];
     Script          = null;
     delegateCache   = new Dictionary <string, Delegate>();
     dwModuleSize    = 0;
     dwLibraryCount  = 0;
     SendPacketD     = null;
     CheckModuleD    = null;
     ModuleLoadD     = null;
     AllocateMemD    = null;
     FreeMemoryD     = null;
     SetRC4DataD     = null;
     GetRC4DataD     = null;
     GenerateRC4Keys = null;
     UnloadModule    = null;
     PacketHandler   = null;
     Tick            = null;
     m_Mod           = 0;
     m_ModMem        = 0;
     InitPointer     = 0;
     init            = null;
     myFuncList      = IntPtr.Zero;
     myFunctionList  = default;
     pFuncList       = 0;
     ppFuncList      = 0;
     myWardenList    = default;
     pWardenList     = 0;
     m_RC4           = 0;
     m_PKT           = Array.Empty <byte>();
 }
Esempio n. 7
0
        private bool InitModule(ref byte[] Data)
        {
            int          A;
            int          B;
            int          C;
            var          bCode = new byte[16];
            MemoryStream ms    = new(Data);
            BinaryReader br    = new(ms);

            Marshal.Copy(Data, 0, new IntPtr(m_Mod), Data.Length);
            br.BaseStream.Position = 0x18L;
            C = br.ReadInt32();
            B = 1 - C;
            br.BaseStream.Position = 0x14L;
            if (B > br.ReadInt32())
            {
                return(false);
            }

            br.BaseStream.Position = 0x10L;
            A = br.ReadInt32();
            br.BaseStream.Position = A + (B * 4);
            A           = br.ReadInt32() + m_Mod;
            InitPointer = A;
            Console.WriteLine("Initialize Function is mapped at 0x{0:X}", InitPointer);
            SendPacketD    = SendPacket;
            CheckModuleD   = CheckModule;
            ModuleLoadD    = ModuleLoad;
            AllocateMemD   = AllocateMem;
            FreeMemoryD    = FreeMemory;
            SetRC4DataD    = SetRC4Data;
            GetRC4DataD    = GetRC4Data;
            myFunctionList = new FuncList
            {
                fpSendPacket     = Marshal.GetFunctionPointerForDelegate(SendPacketD).ToInt32(),
                fpCheckModule    = Marshal.GetFunctionPointerForDelegate(CheckModuleD).ToInt32(),
                fpLoadModule     = Marshal.GetFunctionPointerForDelegate(ModuleLoadD).ToInt32(),
                fpAllocateMemory = Marshal.GetFunctionPointerForDelegate(AllocateMemD).ToInt32(),
                fpReleaseMemory  = Marshal.GetFunctionPointerForDelegate(FreeMemoryD).ToInt32(),
                fpSetRC4Data     = Marshal.GetFunctionPointerForDelegate(SetRC4DataD).ToInt32(),
                fpGetRC4Data     = Marshal.GetFunctionPointerForDelegate(GetRC4DataD).ToInt32()
            };
            Console.WriteLine("Imports: ");
            Console.WriteLine("  SendPacket: 0x{0:X}", myFunctionList.fpSendPacket);
            Console.WriteLine("  CheckModule: 0x{0:X}", myFunctionList.fpCheckModule);
            Console.WriteLine("  LoadModule: 0x{0:X}", myFunctionList.fpLoadModule);
            Console.WriteLine("  AllocateMemory: 0x{0:X}", myFunctionList.fpAllocateMemory);
            Console.WriteLine("  ReleaseMemory: 0x{0:X}", myFunctionList.fpReleaseMemory);
            Console.WriteLine("  SetRC4Data: 0x{0:X}", myFunctionList.fpSetRC4Data);
            Console.WriteLine("  GetRC4Data: 0x{0:X}", myFunctionList.fpGetRC4Data);

            // http://forum.valhallalegends.com/index.php?topic=17758.0
            myFuncList = new IntPtr(malloc(0x1C));
            Marshal.StructureToPtr(myFunctionList, myFuncList, false);
            pFuncList = myFuncList.ToInt32();
            int localVarPtr()
            {
                object argobj = pFuncList; var ret = VarPtr(ref argobj); return(ret);
            }

            ppFuncList = localVarPtr();
            Console.WriteLine("Initializing module");
            init         = (InitializeModule)Marshal.GetDelegateForFunctionPointer(new IntPtr(InitPointer), typeof(InitializeModule));
            m_ModMem     = init.Invoke(ppFuncList);
            pWardenList  = Marshal.ReadInt32(new IntPtr(m_ModMem));
            myWardenList = (WardenFuncList)Marshal.PtrToStructure(new IntPtr(pWardenList), typeof(WardenFuncList));
            Console.WriteLine("Exports:");
            Console.WriteLine("  GenerateRC4Keys: 0x{0:X}", myWardenList.fpGenerateRC4Keys);
            Console.WriteLine("  Unload: 0x{0:X}", myWardenList.fpUnload);
            Console.WriteLine("  PacketHandler: 0x{0:X}", myWardenList.fpPacketHandler);
            Console.WriteLine("  Tick: 0x{0:X}", myWardenList.fpTick);
            GenerateRC4Keys = (GenerateRC4KeysDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpGenerateRC4Keys), typeof(GenerateRC4KeysDelegate));
            UnloadModule    = (UnloadModuleDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpUnload), typeof(UnloadModuleDelegate));
            PacketHandler   = (PacketHandlerDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpPacketHandler), typeof(PacketHandlerDelegate));
            Tick            = (TickDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpTick), typeof(TickDelegate));
            ms.Close();
            ms.Dispose();
            ms = null;
            br = null;
            return(true);
        }
Esempio n. 8
0
 public static int ExecuteCompiled(CustomFieldIdDict compiled, InitializeModule init, string fullName)
 {
     return(ExecuteCompiled(compiled, init, fullName, null));
 }
        public void UpdateForReload(PythonModule reloaded)
        {
            this.__dict__ = reloaded.__dict__;
            this.initialize = reloaded.initialize;

            Initialize();
        }
Esempio n. 10
0
        internal PythonModule(string name, IAttributesDictionary dict, SystemState state, InitializeModule init, CallerContextAttributes callerContextFlags)
        {
            Debug.Assert(state != null);

            __dict__ = dict;
            ModuleName = name;
            __dict__[SymbolTable.Builtins] = TypeCache.Builtin;

            initialize = init;

            contextFlags = callerContextFlags;
            systemState = state;
        }
Esempio n. 11
0
 internal PythonModule(string name, CompiledModule compiledModule, SystemState state, InitializeModule init)
     : this(name, compiledModule, state, init, CallerContextAttributes.None)
 {
 }
        internal PythonModule Load(string moduleName, InitializeModule init, SystemState state)
        {
            InitializeBuiltins();

            PythonModule pmod = new PythonModule(moduleName, this, state, init);
            this.Module = pmod;
            return pmod;
        }
Esempio n. 13
0
 public PythonModule(string name, IDictionary <object, object> dict, SystemState state, InitializeModule init)
     : this(name, dict, state, init, CallerContextFlags.None)
 {
 }