Exemplo n.º 1
0
        private void LoadScripts(string scriptPath)
        {
            const string assemblyPrefix = "Banking_";

            if (string.IsNullOrWhiteSpace(scriptPath))
            {
                return;
            }

            if (!File.Exists(scriptPath))
            {
                BankingPlugin.Instance.LogPrint($"Unable to find script file '{scriptPath}'.", TraceLevel.Error);
                return;
            }

            var scriptName   = Path.GetFileNameWithoutExtension(scriptPath);
            var assemblyName = $"{assemblyPrefix}{scriptName}.dll";
            var scripts      = new List <string>()
            {
                scriptPath
            };
            var compiler = new BooScriptCompiler();

            compiler.Configure(ScriptHelpers.GetReferences(),
                               ScriptHelpers.GetDefaultImports(),
                               ScriptHelpers.GetEnsuredMethodSignatures());
            var context = compiler.Compile(assemblyName, scripts);

            if (context.Errors.Count > 0)
            {
                BankingPlugin.Instance.LogPrintBooErrors(context);
                return;
            }
            else if (context.Warnings.Count > 0)
            {
                BankingPlugin.Instance.LogPrintBooWarnings(context);
            }

            var ass    = context.GeneratedAssembly;
            var linker = new BooModuleLinker(ass, scriptPath);

            ScriptOnPreReward       = linker.TryCreateDelegate <Func <string, Reward, CurrencyDefinition, decimal, decimal> >("OnPreReward");
            scriptOnAccountDeposit  = linker.TryCreateDelegate <Action <Bank, BalanceChangedEventArgs> >("OnAccountDeposit");
            scriptOnAccountWithdraw = linker.TryCreateDelegate <Action <Bank, BalanceChangedEventArgs> >("OnAccountWithdraw");
        }
        private ScriptAssemblyHolder Compile(string scriptPath)
        {
            var bc = new BooScriptCompiler();

            bc.Configure(ScriptHelpers.GetReferences(), ScriptHelpers.GetDefaultImports());

            var name = Path.GetFileNameWithoutExtension(scriptPath);

            var convertStep = new ModuleToInstanceClassStep();

            convertStep.SourceModuleName      = name;
            convertStep.TargetClassName       = $"{name}Quest";
            convertStep.TargetBaseClassName   = "CustomQuests.Quests.Quest";
            convertStep.TargetMethodName      = "OnRun";
            convertStep.TargetMethodModifiers = TypeMemberModifiers.Protected | TypeMemberModifiers.Override;

            var pipe = bc.InternalCompiler.Parameters.Pipeline;

            pipe.InsertAfter(typeof(InjectImportsStep), convertStep);

            var assName = $"Quest_{name}.dll";
            var holder  = new ScriptAssemblyHolder(DateTime.Now);
            var context = bc.Compile(assName, new string[] { scriptPath });

            CustomQuestsPlugin.Instance.LogPrintBooErrors(context);
            CustomQuestsPlugin.Instance.LogPrintBooWarnings(context);

            if (context.Errors.Count == 0)
            {
                holder.Assembly = context.GeneratedAssembly;
                CustomQuestsPlugin.Instance.LogPrint($"Compiled quest {name}", TraceLevel.Info);
            }
            else
            {
                CustomQuestsPlugin.Instance.LogPrint($"Failed to compile {name}.", TraceLevel.Error);
            }

            return(holder);
        }