public static void GenerateStaticCollectionScript(ScriptableObjectCollection collection)
        {
            string fileName = $"{collection.GetCollectionType().Name}Static";
            bool   isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings.Instance.IsGeneratingCustomStaticFile(collection);

            if (isGeneratingCustomStaticFile)
            {
                fileName = ScriptableObjectCollectionSettings.Instance.GetGeneratedStaticFileName(collection);
            }

            string nameSpace   = collection.GetCollectionType().Namespace;
            string finalFolder = ScriptableObjectCollectionSettings.Instance.GetStaticFileFolderForCollection(collection);

            if (string.IsNullOrEmpty(finalFolder))
            {
                Debug.LogError("Static Code Generation folder not assigned, please assign it on the ScriptableObjectCollectionSettings");
                EditorGUIUtility.PingObject(ScriptableObjectCollectionSettings.Instance);
                Selection.objects = new Object[] { ScriptableObjectCollectionSettings.Instance };
                return;
            }

            AssetDatabaseUtils.CreatePathIfDontExist(finalFolder);
            using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs")))
            {
                int indentation = 0;

                List <string> directives = new List <string>();
                directives.Add(typeof(CollectionsRegistry).Namespace);
                directives.Add(collection.GetType().Namespace);
                directives.AddRange(GetCollectionDirectives(collection));

                if (!isGeneratingCustomStaticFile)
                {
                    AppendHeader(writer, ref indentation, nameSpace, "",
                                 collection.GetCollectionType().Name, true, false, directives.Distinct().ToArray());
                }
                else
                {
                    AppendHeader(writer, ref indentation, nameSpace, "",
                                 fileName, false, false, directives.Distinct().ToArray());
                }

                GeneratedStaticFileType staticFileTypeForCollection = ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(collection);
                if (staticFileTypeForCollection == GeneratedStaticFileType.DirectAccess)
                {
                    WriteDirectAccessCollectionStatic(collection, writer, ref indentation);
                }
                else
                {
                    WriteTryGetAccessCollectionStatic(collection, writer, ref indentation);
                }

                indentation--;
                AppendFooter(writer, ref indentation, nameSpace);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
Exemple #2
0
        public static void GenerateStaticCollectionScript(ScriptableObjectCollection collection)
        {
            CollectionsRegistry.Instance.ValidateGUIDs();
            if (!CanGenerateStaticFile(collection, out string errorMessage))
            {
                Debug.LogError(errorMessage);
                return;
            }

            SerializedObject collectionSerializedObject = new SerializedObject(collection);


            string fileName  = collectionSerializedObject.FindProperty("generatedStaticClassFileName").stringValue;
            string nameSpace = collectionSerializedObject.FindProperty("generateStaticFileNamespace").stringValue;

            string finalFolder    = collectionSerializedObject.FindProperty("generatedFileLocationPath").stringValue;
            bool   writeAsPartial = collectionSerializedObject.FindProperty("generateAsPartialClass").boolValue;


            AssetDatabaseUtils.CreatePathIfDontExist(finalFolder);
            using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs")))
            {
                int indentation = 0;

                List <string> directives = new List <string>();
                directives.Add(typeof(CollectionsRegistry).Namespace);
                directives.Add(collection.GetType().Namespace);
                directives.Add(typeof(List <>).Namespace);
                directives.Add("System.Linq");
                directives.AddRange(GetCollectionDirectives(collection));
                string className = collection.GetItemType().Name;
                if (!writeAsPartial)
                {
                    className = fileName;
                }

                AppendHeader(writer, ref indentation, nameSpace, "", className,
                             writeAsPartial,
                             false, directives.Distinct().ToArray()
                             );

                WriteDirectAccessCollectionStatic(collection, writer, ref indentation);

                indentation--;
                AppendFooter(writer, ref indentation, nameSpace);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
                                                              ref int indentation)
        {
            bool isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings.Instance.IsGeneratingCustomStaticFile(collection);

            string cachedValuesName = "values";

            AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                AppendLine(writer, indentation,
                           $"private static {collectionItem.GetType().Name} {collectionItem.name.Sanitize().FirstToLower()};");
            }

            AppendLine(writer, indentation);

            string valuesName = $"Values";

            if (!isGeneratingCustomStaticFile)
            {
                AppendLine(writer, indentation,
                           $"public static {collection.GetType().Name} {valuesName}");
            }
            else
            {
                AppendLine(writer, indentation,
                           $"public static {collection.GetType().Name} {valuesName}");
            }
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, "get");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"if ({cachedValuesName} == null)");
            indentation++;
            AppendLine(writer, indentation,
                       $"{cachedValuesName} = ({collection.GetType()})CollectionsRegistry.Instance.GetCollectionByGUID(\"{collection.GUID}\");");
            indentation--;
            AppendLine(writer, indentation, $"return {cachedValuesName};");
            indentation--;
            AppendLine(writer, indentation, "}");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);

            AppendLine(writer, indentation);

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                string collectionNameFirstUpper            = collectionItem.name.Sanitize().FirstToUpper();
                string privateStaticName = collectionItem.name.Sanitize().FirstToLower();

                AppendLine(writer, indentation,
                           $"public static {collectionItem.GetType().Name} {collectionNameFirstUpper}");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "get");
                AppendLine(writer, indentation, "{");
                indentation++;

                AppendLine(writer, indentation, $"if ({privateStaticName} == null)");
                indentation++;
                AppendLine(writer, indentation,
                           $"{privateStaticName} = ({collectionItem.GetType().Name}){valuesName}.GetCollectableByGUID(\"{collectionItem.GUID}\");");
                indentation--;
                AppendLine(writer, indentation, $"return {privateStaticName};");
                indentation--;
                AppendLine(writer, indentation, "}");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);
            }
        }
        private static void WriteTryGetAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
                                                              ref int indentation)
        {
            string cachedValuesName = $"values";
            string valuesName       = $"Values";
            string tryGetValuesName = $"TryGet{valuesName}";

            AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                AppendLine(writer, indentation,
                           $"private static {collectionItem.GetType().Name} {collectionItem.name.Sanitize().FirstToLower()};");
            }

            AppendLine(writer, indentation);

            AppendLine(writer, indentation, $"public static bool {tryGetValuesName}(out {collection.GetType().Name} result)");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"if ({cachedValuesName} != null)");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"result = {cachedValuesName};");
            AppendLine(writer, indentation, "return true;");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);
            AppendLine(writer, indentation, $"if (!CollectionsRegistry.Instance.TryGetCollectionByGUID(\"{collection.GUID}\", out ScriptableObjectCollection resultCollection))");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"result = {cachedValuesName};");
            AppendLine(writer, indentation, "return false;");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);
            AppendLine(writer, indentation, $"{cachedValuesName} = ({collection.GetType().Name}) resultCollection;");
            AppendLine(writer, indentation, $"result = {cachedValuesName};");
            AppendLine(writer, indentation, $"return true;");
            indentation--;
            AppendLine(writer, indentation, "}");

            AppendLine(writer, indentation);

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                string pascalizedItemName = collectionItem.name.Sanitize().FirstToUpper();
                string camelizedItemName  = collectionItem.name.Sanitize().FirstToLower();
                Type   itemType           = collectionItem.GetType();


                AppendLine(writer, indentation, $"public static bool TryGet{pascalizedItemName}(out {itemType.Name} result)");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, $"if ({camelizedItemName} != null)");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, $"result = {camelizedItemName};");
                AppendLine(writer, indentation, "return true;");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);

                AppendLine(writer, indentation, $"if (!{tryGetValuesName}(out {collection.GetType().Name} collectionResult))");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "result = null;");
                AppendLine(writer, indentation, "return false;");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);


                AppendLine(writer, indentation, $"if (!collectionResult.TryGetCollectableByGUID(\"{collectionItem.GUID}\", out CollectableScriptableObject resultCollectable))");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "result = null;");
                AppendLine(writer, indentation, "return false;");
                indentation--;
                AppendLine(writer, indentation);
                AppendLine(writer, indentation, "}");


                AppendLine(writer, indentation, $"{camelizedItemName} = ({itemType.Name}) resultCollectable;");
                AppendLine(writer, indentation, $"result = {camelizedItemName};");
                AppendLine(writer, indentation, "return true;");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);
            }
        }
Exemple #5
0
        private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
                                                              ref int indentation)
        {
            string cachedValuesName = "values";

            AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");

            AppendLine(writer, indentation);

            for (int i = 0; i < collection.Items.Count; i++)
            {
                ScriptableObjectCollectionItem collectionItem = collection.Items[i];
                AppendLine(writer, indentation,
                           $"private static {collectionItem.GetType().Name} {collectionItem.name.Sanitize().FirstToLower()};");
            }

            AppendLine(writer, indentation);

            string valuesName = $"Values";

            AppendLine(writer, indentation,
                       $"public static {collection.GetType().Name} {valuesName}");

            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, "get");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"if ({cachedValuesName} == null)");
            indentation++;
            AppendLine(writer, indentation,
                       $"{cachedValuesName} = ({collection.GetType()})CollectionsRegistry.Instance.GetCollectionByGUID(\"{collection.GUID}\");");
            indentation--;
            AppendLine(writer, indentation, $"return {cachedValuesName};");
            indentation--;
            AppendLine(writer, indentation, "}");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);

            AppendLine(writer, indentation);

            for (int i = 0; i < collection.Items.Count; i++)
            {
                ScriptableObjectCollectionItem collectionItem = collection.Items[i];
                string collectionNameFirstUpper = collectionItem.name.Sanitize().FirstToUpper();
                string privateStaticName        = collectionItem.name.Sanitize().FirstToLower();

                AppendLine(writer, indentation,
                           $"public static {collectionItem.GetType().Name} {collectionNameFirstUpper}");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "get");
                AppendLine(writer, indentation, "{");
                indentation++;

                AppendLine(writer, indentation, $"if ({privateStaticName} == null)");
                indentation++;
                AppendLine(writer, indentation,
                           $"{privateStaticName} = ({collectionItem.GetType().Name}){valuesName}.GetItemByGUID(\"{collectionItem.GUID}\");");
                indentation--;
                AppendLine(writer, indentation, $"return {privateStaticName};");
                indentation--;
                AppendLine(writer, indentation, "}");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);
            }


            AppendLine(writer, indentation, $"public static IEnumerable<T> GetValues<T>() where T : {collection.GetItemType().Name}");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"return Values.Where(item => item is T).Cast<T>();");
            indentation--;
            AppendLine(writer, indentation, "}");

            AppendLine(writer, indentation);
        }