protected override void DoRefreshProgramActions()
        {
            // ToDo: сейчас берет кэш, а если кэша нету, то генерирует новое апи.
            // Нужно найти какое-то событие эдитора - при первом запуске эдитора делать принудительную генерацию апи и ложить ее в кэш (ну или ставить какой-то глобальный флаг что первое
            // получение апи надо сделать с помощью генерации)
            // то есть должна быть гарантия что после перезапуска юнити апи будет пере-генерировано на основе актуального sdk, а не взято из кэша (т.к. там может быть старый sdk)

            var externalApiVersion = VRC.Core.SDKClientUtilities.GetSDKVersionDate();

            var generator = new DoshikExternalApiGenerator
            {
                LogWarning = (text) => { UnityEngine.Debug.LogWarning("Doshik: Generate Udon API: " + text); },
                LogInfo    = (text) => { UnityEngine.Debug.Log("Doshik: Generate Udon API: " + text); }
            };

            var externalApi = generator.GetOrGenerateCache(externalApiVersion);

            var compiler = new Compiler();

            compiler.ExternalApi = externalApi;

            compiler.SourceCode = _sourceCode;

            try
            {
                var output = compiler.Compile();

                if (output.CompilationErrors == null)
                {
                    var assemblyError = AssembleProgram(output.UdonAssemblyCode);

                    if (assemblyError != null)
                    {
                        CompilationError = "Assembly: " + assemblyError;
                        UnityEngine.Debug.LogError("Doshik: " + CompilationError);

                        program = null;
                    }
                    else
                    {
                        base.DoRefreshProgramActions();

                        ApplyDefaultValuesToHeap(TranslateDefaultValues(output.DefaultHeapValues));
                    }
                }
                else
                {
                    foreach (var error in output.CompilationErrors)
                    {
                        CompilationError = "Compilation: " + error;
                        UnityEngine.Debug.LogError("Doshik: " + CompilationError);

                        program = null;
                    }
                }
            }
            catch (Exception ex)
            {
                CompilationError = "Unexpected exception while compiling: " + ex.ToString();
                UnityEngine.Debug.LogError("Doshik: " + CompilationError);

                program = null;
            }
        }
Beispiel #2
0
        static void Main()
        {
            var projectDir     = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\"));
            var sourceFilePath = Path.Combine(projectDir, "test.doshik");

            var source = File.ReadAllText(sourceFilePath);

            Console.WriteLine(source);
            Console.WriteLine();

            var generator = new DoshikExternalApiGenerator
            {
                LogWarning = (text) => { Console.WriteLine("WARNING: " + text); },
                LogInfo    = (text) => { Console.WriteLine("INFO: " + text); }
            };

            var externalApi = generator.GetOrGenerateCache("test");

            var compiler = new Compiler()
            {
                ExternalApi = externalApi
            };

            compiler.SourceCode = source;

            Console.WriteLine("compiling...");

            var output = compiler.Compile();

            if (output.CompilationErrors == null)
            {
                Console.WriteLine("compiled.");

                Console.WriteLine("code:");
                Console.WriteLine();
                Console.WriteLine("###############################################################");
                Console.WriteLine(output.UdonAssemblyCode);
                Console.WriteLine("###############################################################");

                Console.WriteLine();
                Console.WriteLine("variable default values:");
                Console.WriteLine();
                foreach (var variable in output.DefaultHeapValues)
                {
                    if (variable.Value is ConcreteValueDefaultHeapValue concreteValue)
                    {
                        Console.WriteLine(variable.Key + " = [value] '" + concreteValue.Value.ToString() + "' (" + concreteValue.Type.FullName + ")");
                    }
                    else if (variable.Value is TypeAsStringDefaultHeapValue typeAsString)
                    {
                        Console.WriteLine(variable.Key + " = [type] '" + typeAsString.TypeAsString + "'");
                    }
                    else
                    {
                        Console.WriteLine(variable.Key + " = ???");
                    }
                }
            }
            else
            {
                Console.WriteLine("compilation ERRORS:");

                foreach (var error in output.CompilationErrors)
                {
                    Console.WriteLine($"{ error.LineIdx + 1 }, { error.CharInLineIdx + 1 }: { error.Message }");
                }
            }

            Console.ReadLine();
        }