Example #1
0
        private List <string> GetDLLNamesFromAsmdef(ObjectBuildDescription buildObject)
        {
            string asmdefFilePath = buildObject.FolderPath + "/" + buildObject.ObjectName + ".asmdef";
            var    dllNamesList   = new List <string>(0);

            if (!File.Exists(asmdefFilePath))
            {
                Debug.LogError("No .asmdef file found for " + buildObject.ObjectName + " at " + asmdefFilePath);

                return(dllNamesList);
            }

            string asmdefText             = File.ReadAllText(asmdefFilePath);
            AssemblyDefinitionData asmdef = JsonConvert.DeserializeObject <AssemblyDefinitionData>(asmdefText);

            dllNamesList = new List <string>(asmdef.references.Length - 1);
            dllNamesList.Add(buildObject.ObjectName + "_" + buildObject.ObjectGuid.Replace("-", "") + ".dll");

            foreach (string dllName in asmdef.references)
            {
                if (dllName != "VarwinCore")
                {
                    dllNamesList.Add(dllName + ".dll");
                }
            }

            return(dllNamesList);
        }
Example #2
0
        private void GenerateWrappers()
        {
            if (_state != ObjectBuilderState.GeneratingWrapper)
            {
                _state = ObjectBuilderState.GeneratingWrapper;
                _currentObjectIndex = 0;
            }

            if (_currentObjectIndex >= _objectsToBuild.Count)
            {
                GoToTheNextBuildState();
                _currentObjectIndex = 0;

                string jsonModels = JsonConvert.SerializeObject(_objectsToBuild.ToArray(), Formatting.None,
                                                                new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });

                File.WriteAllText(TEMP_FILE_NAME, jsonModels);

                AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

                return;
            }

            _currentStatusString = "Generating wrappers...";

            ObjectBuildDescription buildObject = _objectsToBuild[_currentObjectIndex];

            WrapperGenerator.GenerateWrapper(buildObject.ContainedObjectDescriptor);

            _currentObjectIndex++;
        }
Example #3
0
 private void BuildOnlyZip(ObjectBuildDescription buildObject)
 {
     _state           = ObjectBuilderState.CreatingFiles;
     _stopAfterOneTry = true;
     _objectsToBuild  = new List <ObjectBuildDescription>();
     _objectsToBuild.Add(buildObject);
 }
Example #4
0
 private void BuildOnlyAssetBundle(ObjectBuildDescription buildObject)
 {
     _state           = ObjectBuilderState.BuildingAssetBundles;
     _stopAfterOneTry = true;
     _objectsToBuild  = new List <ObjectBuildDescription>();
     _objectsToBuild.Add(buildObject);
 }
Example #5
0
 private void BuildOnlyWrapper(ObjectBuildDescription buildObject)
 {
     _state           = ObjectBuilderState.GeneratingWrapper;
     _stopAfterOneTry = true;
     _objectsToBuild  = new List <ObjectBuildDescription>();
     _objectsToBuild.Add(buildObject);
 }
Example #6
0
        private void CreateAssetBundleConfig(GameObject go, ObjectBuildDescription buildObject)
        {
            AssetInfo assetInfo =
                new AssetInfo {
                AssetName = go.name, Assembly = GetDLLNamesFromAsmdef(buildObject)
            };

            string jsonConfig = JsonConvert.SerializeObject(assetInfo, Formatting.None,
                                                            new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            buildObject.ContainedObjectDescriptor.ConfigAssetBundle = jsonConfig;
            buildObject.ConfigAssetBundle = jsonConfig;
        }
Example #7
0
        public static string CreateBlocklyConfig(Wrapper wrapper, Type type, ObjectBuildDescription objectBuild)
        {
            if (!Initialize(wrapper, type, objectBuild))
            {
                return(null);
            }

            AddPropertiesToConfig();
            AddFieldsToConfig();
            AddMethodsToConfig();
            AddEventsToConfig();

            var jsonSerializerSettings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            string jsonConfig = JsonConvert.SerializeObject(_config, Formatting.None, jsonSerializerSettings);

            return(jsonConfig);
        }
Example #8
0
        private void RunBuildObjects(CreateObjectModel[] objects)
        {
            try
            {
                _objectsToBuild      = new List <ObjectBuildDescription>();
                _state               = ObjectBuilderState.Preparing;
                _currentStatusString = "Preparing objects...";

                foreach (CreateObjectModel model in objects)
                {
                    GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(model.PrefabPath);

                    if (prefab != null)
                    {
                        VarwinObjectDescriptor objToBuild = prefab.GetComponent <VarwinObjectDescriptor>();

                        if (objToBuild != null)
                        {
                            PreBuild(prefab);

                            ObjectBuildDescription descr = new ObjectBuildDescription()
                            {
                                ObjectName = objToBuild.Name,
                                ObjectGuid = objToBuild.RootGuid,
                                PrefabPath = model.PrefabPath,
                                FolderPath = model.ObjectFolder,
                                ContainedObjectDescriptor = objToBuild
                            };

                            _objectsToBuild.Add(descr);
                        }
                    }
                }

                GenerateWrappers();
            }
            catch (Exception e)
            {
                EditorUtility.DisplayDialog("Error!", $"{e.Message}:\nProblem when build objects", "OK");
                Debug.LogException(e);
                Close();
            }
        }
Example #9
0
        private static bool Initialize(Wrapper wrapper, Type type, ObjectBuildDescription objectBuild)
        {
            if (string.IsNullOrWhiteSpace(type.FullName))
            {
                return(false);
            }

            _wrapperType = wrapper.GetType();
            Debug.Log($"<Color=Green><b>Wrapper {_wrapperType.Name} is loaded!</b></Color>");

            VarwinObjectDescriptor varwinObjectDescriptor = objectBuild.ContainedObjectDescriptor;

            Debug.Log($"Core version is {VarwinVersionInfo.VarwinVersion}");

            string builtAt = $"{DateTimeOffset.UtcNow:s}Z";

            if (DateTimeOffset.TryParse(varwinObjectDescriptor.BuiltAt, out DateTimeOffset builtAtDateTimeOffset))
            {
                builtAt = $"{builtAtDateTimeOffset.UtcDateTime:s}Z";
            }

            _config = new BlocklyConfig
            {
                Guid        = varwinObjectDescriptor.Guid,
                RootGuid    = varwinObjectDescriptor.RootGuid,
                Embedded    = varwinObjectDescriptor.Embedded,
                MobileReady = SdkSettings.MobileFeature.Enabled && varwinObjectDescriptor.MobileReady,
                Config      = new Config
                {
                    type   = $"{varwinObjectDescriptor.Name}_{varwinObjectDescriptor.RootGuid.Replace("-", "")}.{varwinObjectDescriptor.Name}Wrapper",
                    blocks = new List <Block>(),
                },
                Author = new JsonAuthor
                {
                    Name  = varwinObjectDescriptor.AuthorName,
                    Email = varwinObjectDescriptor.AuthorEmail,
                    Url   = varwinObjectDescriptor.AuthorUrl,
                },
                BuiltAt = $"{builtAt}",
                License = new JsonLicense
                {
                    Code    = varwinObjectDescriptor.LicenseCode,
                    Version = varwinObjectDescriptor.LicenseVersion,
                },
                SdkVersion = VarwinVersionInfo.VersionNumber
            };

            if (File.Exists(objectBuild.TagsPath))
            {
                var tags = File.ReadAllLines(objectBuild.TagsPath);
                _config.Tags = tags.ToList();
            }

            if (_config.Config.i18n == null)
            {
                _config.Config.i18n = new I18n();
            }
            var locales = type.GetCustomAttributes <LocaleAttribute>(true);

            foreach (var locale in locales)
            {
                if (locale.I18n != null)
                {
                    _config.Config.i18n = locale.I18n;
                    break;
                }
                else
                {
                    _config.Config.i18n.SetLocale(locale.Code, locale.Strings[0]);
                }
            }
            return(true);
        }
Example #10
0
        private void CreateFiles()
        {
            string assetBundleDirectory = Application.dataPath.Replace("Assets", "AssetBundles");
            string bakedObjectDirectory = Application.dataPath.Replace("Assets", "BakedObjects");
            string dllFolder            = Application.dataPath.Replace("Assets", "Library") + $"/ScriptAssemblies";

            string        tempBundleFile         = bakedObjectDirectory + "/bundle";
            string        tempBundleManifestFile = bakedObjectDirectory + "/bundle.manifest";
            string        tempBundleJsonFile     = bakedObjectDirectory + "/bundle.json";
            string        tempBundleIconFile     = bakedObjectDirectory + "/bundle.png";
            string        tempInstallJsonFile    = bakedObjectDirectory + "/install.json";
            List <string> tempDllFiles           = new List <string>();
            List <string> newDllPaths            = new List <string>();
            List <string> filesToZip             = new List <string>();

            string tempAndroidBundleFile         = bakedObjectDirectory + "/android_bundle";
            string tempAndroidBundleManifestFile = bakedObjectDirectory + "/android_bundle.manifest";

            try
            {
                if (_objectsToBuild == null)
                {
                    return;
                }

                if (_currentObjectIndex >= _objectsToBuild.Count)
                {
                    GoToTheNextBuildState();
                    _currentObjectIndex = 0;

                    return;
                }

                if (EditorApplication.isCompiling)
                {
                    return;
                }

                ObjectBuildDescription buildObject = _objectsToBuild[_currentObjectIndex];

                _currentStatusString = "Baking object to " + bakedObjectDirectory;
                Debug.Log("Baking object to " + bakedObjectDirectory);
                Repaint();

                if (!Directory.Exists(bakedObjectDirectory))
                {
                    try
                    {
                        Directory.CreateDirectory(bakedObjectDirectory);
                    }

                    catch
                    {
                        Debug.LogError("Can't create object directory for " + buildObject.ObjectName);

                        EditorUtility.DisplayDialog("Can't create objects",
                                                    "Please, close all your explorer windows and press OK to try again.",
                                                    "OK");

                        return;
                    }
                }

                var dllNamesList = GetDLLNamesFromAsmdef(buildObject);
                tempDllFiles = new List <string>(dllNamesList.Count);
                foreach (string dllName in dllNamesList)
                {
                    tempDllFiles.Add(dllFolder + $"/" + dllName);
                }

                string bundlePath = assetBundleDirectory +
                                    "/" +
                                    buildObject.ObjectName.ToLower() +
                                    "_" +
                                    buildObject.ObjectGuid.Replace("-", "");

                string bundleManifestPath = assetBundleDirectory +
                                            "/" +
                                            buildObject.ObjectName.ToLower() +
                                            "_" +
                                            buildObject.ObjectGuid.Replace("-", "") +
                                            ".manifest";

                string androidBundlePath = assetBundleDirectory +
                                           "/android_" +
                                           buildObject.ObjectName.ToLower() +
                                           "_" +
                                           buildObject.ObjectGuid.Replace("-", "");

                string androidBundleManifestPath = assetBundleDirectory +
                                                   "/android_" +
                                                   buildObject.ObjectName.ToLower() +
                                                   "_" +
                                                   buildObject.ObjectGuid.Replace("-", "") +
                                                   ".manifest";

                newDllPaths = new List <string>(tempDllFiles.Count);
                foreach (string tempDllFile in tempDllFiles)
                {
                    string dllPath = tempDllFile;

                    string newDllPath = bakedObjectDirectory + $"/" + Path.GetFileName(tempDllFile);
                    newDllPaths.Add(newDllPath);

                    if (!File.Exists(tempDllFile))
                    {
                        string oldSupportedFile =
                            tempDllFile.Replace("_" + buildObject.ObjectGuid.Replace("-", ""), "");

                        if (!File.Exists(oldSupportedFile))
                        {
                            Debug.LogError("Can't find " + tempDllFile);

                            EditorUtility.DisplayDialog(buildObject.ObjectName + " Error!",
                                                        "Can't build object. File hasn't been compiled: " +
                                                        tempDllFile +
                                                        ". Please check your assembly name.", "OK");
                            _currentObjectIndex++;

                            return;
                        }
                        else
                        {
                            dllPath = oldSupportedFile;

                            Debug.LogWarning(
                                "WARNING! Old object detected. Please, change your asmdef to <objectname>_<objectguidwithoutdashes> format! Now using file " +
                                dllPath);
                        }
                    }

                    if (File.Exists(newDllPath))
                    {
                        File.Delete(newDllPath);
                        Debug.LogWarning($"WARNING! Old DLL detected. Dll file delete {dllPath}");
                    }

                    File.Copy(dllPath, newDllPath);
                }

                File.Copy(bundlePath, tempBundleFile);
                File.Copy(bundleManifestPath, tempBundleManifestFile);

                if (SdkSettings.MobileFeature.Enabled && buildObject.ContainedObjectDescriptor.MobileReady)
                {
                    File.Copy(androidBundlePath, tempAndroidBundleFile);
                    File.Copy(androidBundleManifestPath, tempAndroidBundleManifestFile);
                }

                if (File.Exists(buildObject.IconPath))
                {
                    File.Copy(buildObject.IconPath, tempBundleIconFile);
                }

                File.WriteAllText(tempInstallJsonFile, buildObject.ConfigBlockly);
                File.WriteAllText(tempBundleJsonFile, buildObject.ConfigAssetBundle);

                filesToZip = new List <string>()
                {
                    tempBundleFile,
                    tempBundleIconFile,
                    tempBundleJsonFile,
                    tempBundleManifestFile,
                    tempInstallJsonFile
                };

                if (SdkSettings.MobileFeature.Enabled && buildObject.ContainedObjectDescriptor.MobileReady)
                {
                    filesToZip.Add(tempAndroidBundleFile);
                    filesToZip.Add(tempAndroidBundleManifestFile);
                }

                filesToZip.AddRange(newDllPaths);

                ZipFiles(filesToZip, bakedObjectDirectory + $"/{buildObject.ObjectName}.vwo");

                ObjectBuilt?.Invoke(this, EventArgs.Empty);

                _currentObjectIndex++;
            }
            catch (Exception e)
            {
                EditorUtility.DisplayDialog("Error!", $"{e.Message}:\nProblem when creating files", "OK");
                Debug.LogException(e);
                Close();
            }
            finally
            {
                var files = new List <string>(filesToZip);
                files.AddRange(newDllPaths);
                foreach (var file in filesToZip)
                {
                    try
                    {
                        if (File.Exists(file))
                        {
                            File.Delete(file);
                        }
                    }
                    catch (Exception e)
                    {
                        EditorUtility.DisplayDialog("Error!", $"Can't delete \"{file}\"", "OK");
                        Debug.LogException(e);
                    }
                }
            }
        }
Example #11
0
        private void CreateBundlesConfig()
        {
            if (_currentObjectIndex >= _objectsToBuild.Count)
            {
                GoToTheNextBuildState();
                _currentObjectIndex = 0;

                return;
            }

            ObjectBuildDescription buildObject = _objectsToBuild[_currentObjectIndex];

            if (buildObject.ContainedObjectDescriptor == null)
            {
                buildObject.ContainedObjectDescriptor = AssetDatabase.LoadAssetAtPath <GameObject>(buildObject.PrefabPath).GetComponent <VarwinObjectDescriptor>();
            }

            GameObject gameObject = buildObject.ContainedObjectDescriptor.gameObject;

            if (gameObject == null)
            {
                Debug.LogError("Can't create object " + buildObject.ObjectName + ": no game object found!");
                _objectsToBuild.RemoveAt(_currentObjectIndex);

                return;
            }

            _currentStatusString = "Creating configurations for " + buildObject.ObjectName;
            Repaint();

            MonoBehaviour[] myComponents = gameObject.GetComponentsInChildren <MonoBehaviour>(true);
            int             wrappers     = 0;

            foreach (MonoBehaviour myComp in myComponents)
            {
                if (myComp == null)
                {
                    continue;
                }

                Type myObjectType = myComp.GetType();

                if (myObjectType.Assembly.ManifestModule.Name == "VarwinCore.dll" && myObjectType != typeof(JointPoint))
                {
                    continue;
                }

                if (myObjectType.Assembly.ManifestModule.Name.Contains("UnityEngine"))
                {
                    continue;
                }

                if (myObjectType.ImplementsInterface <IWrapperAware>())
                {
                    Wrapper wrapper = gameObject.GetComponentInChildren <IWrapperAware>().Wrapper();

                    buildObject.ContainedObjectDescriptor.ConfigBlockly =
                        BlocklyBuilder.CreateBlocklyConfig(wrapper, myObjectType, buildObject);
                    buildObject.ConfigBlockly = buildObject.ContainedObjectDescriptor.ConfigBlockly;
                    wrappers++;
                }
            }

            if (wrappers == 0)
            {
                EditorUtility.DisplayDialog("Message",
                                            $"IWrapperAware not found on " + buildObject.ContainedObjectDescriptor.Name + "! Please add VarwinObject inheritor script to object.",
                                            "OK");
                _objectsToBuild.RemoveAt(_currentObjectIndex);

                return;
            }

            if (wrappers > 1)
            {
                EditorUtility.DisplayDialog("Message",
                                            $"Can't build " + buildObject.ContainedObjectDescriptor.Name + ": Only one class with IWapperAware interface can be added to an object",
                                            "OK");
                _objectsToBuild.RemoveAt(_currentObjectIndex);

                return;
            }

            CreateAssetBundleConfig(gameObject, buildObject);

            _currentObjectIndex++;
        }
Example #12
0
        private bool CreateIcons()
        {
            if (_state != ObjectBuilderState.CreatingIcons)
            {
                _state = ObjectBuilderState.CreatingIcons;
                _currentObjectIndex = 0;
                AssetPreview.SetPreviewTextureCacheSize(_objectsToBuild.Count + 1);
                _bad_attempts = 0;
            }

            if (_currentObjectIndex >= _objectsToBuild.Count)
            {
                GoToTheNextBuildState();
                _currentObjectIndex = 0;

                return(true);
            }

            if (_currentObjectIndex == 0)
            {
                AssetPreview.SetPreviewTextureCacheSize(_objectsToBuild.Count + 1);
                _bad_attempts = 0;
            }

            ObjectBuildDescription buildObject = _objectsToBuild[_currentObjectIndex];

            if (buildObject.ContainedObjectDescriptor == null)
            {
                buildObject.ContainedObjectDescriptor = AssetDatabase.LoadAssetAtPath <GameObject>(buildObject.PrefabPath).GetComponent <VarwinObjectDescriptor>();
            }

            if (buildObject.ContainedObjectDescriptor.Icon != null)
            {
                GoToTheNextBuildState();
                _currentObjectIndex = 0;
                return(true);
            }

            _currentStatusString = "Creating icon for " + buildObject.ObjectName;

            EditorGUIUtility.PingObject(buildObject.ContainedObjectDescriptor.gameObject);

            int instanceId = buildObject.ContainedObjectDescriptor.gameObject.GetInstanceID();

            Texture2D texture2D = AssetPreview.GetAssetPreview(buildObject.ContainedObjectDescriptor.gameObject);

            if (texture2D == null)
            {
                Repaint();
                _bad_attempts++;

                if (_bad_attempts >= MAX_BAD_ATTEMPTS)
                {
                    if (File.Exists(buildObject.IconPath))
                    {
                        Texture2D icon = AssetDatabase.LoadAssetAtPath <Texture2D>(buildObject.IconPath);

                        if (icon != null)
                        {
                            buildObject.ContainedObjectDescriptor.Icon = icon;
                            Debug.LogWarning("Existent icon was used for " + buildObject.ObjectName);

                            _bad_attempts = 0;
                            _currentObjectIndex++;

                            return(true);
                        }
                    }

                    Debug.LogWarning("Can't create icon for " + buildObject.ObjectName + ". Using default icon.");
                    Texture2D defaultIcon = AssetDatabase.LoadAssetAtPath <Texture2D>(DEFAULT_ICON_PATH);
                    var       bytes       = defaultIcon.EncodeToPNG();
                    File.WriteAllBytes(buildObject.IconPath, bytes);
                    AssetDatabase.ImportAsset(buildObject.IconPath);
                    Texture2D tex = AssetDatabase.LoadAssetAtPath <Texture2D>(buildObject.IconPath);
                    buildObject.ContainedObjectDescriptor.Icon = tex;

                    _bad_attempts = 0;
                    _currentObjectIndex++;

                    return(true);
                }

                return(false);
            }

            var iconBytes = texture2D.EncodeToPNG();

            File.WriteAllBytes(buildObject.IconPath, iconBytes);
            AssetDatabase.ImportAsset(buildObject.IconPath);
            Texture2D te = AssetDatabase.LoadAssetAtPath <Texture2D>(buildObject.IconPath);

            buildObject.ContainedObjectDescriptor.Icon = te;

            Debug.Log("Icon was created for " + buildObject.ObjectName + "!");

            _currentObjectIndex++;

            return(true);
        }
Example #13
0
        private void RunBuildAllObjects()
        {
            try
            {
                _objectsToBuild      = new List <ObjectBuildDescription>();
                _state               = ObjectBuilderState.Preparing;
                _currentStatusString = "Preparing objects...";

                var objectsPath = "Assets/Objects";
                if (!Directory.Exists(objectsPath))
                {
                    Directory.CreateDirectory(objectsPath);
                    try
                    {
                        Directory.CreateDirectory(objectsPath);
                    }
                    catch
                    {
                        Debug.LogError("Can't create directory \"" + objectsPath + "\"");
                        EditorUtility.DisplayDialog("Can't create directory",
                                                    "Can't create directory \"" + objectsPath + "\"",
                                                    "OK");
                        return;
                    }
                }

                foreach (string objectDir in Directory.EnumerateDirectories("Assets/Objects"))
                {
                    foreach (string objectPath in Directory.EnumerateFiles(objectDir, "*.prefab"))
                    {
                        GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(objectPath);

                        if (prefab != null)
                        {
                            VarwinObjectDescriptor objToBuild = prefab.GetComponent <VarwinObjectDescriptor>();

                            if (objToBuild != null)
                            {
                                PreBuild(prefab);

                                //Finally, it's our prefab to build
                                //Debug.Log("Building object " + objToBuild.Name);

                                ObjectBuildDescription descr = new ObjectBuildDescription()
                                {
                                    ObjectName = objToBuild.Name,
                                    ObjectGuid = objToBuild.RootGuid,
                                    PrefabPath = objectPath,
                                    FolderPath = objectDir,
                                    ContainedObjectDescriptor = objToBuild
                                };

                                _objectsToBuild.Add(descr);
                            }
                        }
                    }
                }

                GenerateWrappers();
            }
            catch (Exception e)
            {
                EditorUtility.DisplayDialog("Error!", $"{e.Message}:\nProblem when run build all objects", "OK");
                Debug.LogException(e);
                Close();
            }
        }