public virtual void GenerateCode(LdtkJson ldtkJson, LdtkGeneratorContext ctx, CodeOutput output)
        {
            List <CompilationUnitFragment> fragments = new List <CompilationUnitFragment>();

            foreach (EnumDefinition ed in ldtkJson.Defs.Enums)
            {
                fragments.Add(GenerateEnum(ed, ctx));
            }

            foreach (EntityDefinition ed in ldtkJson.Defs.Entities)
            {
                CompilationUnitClass entity = GenerateEntity(ed, ctx);
                fragments.Add(entity);

                if (ctx.CodeCustomizer != null)
                {
                    ctx.CodeCustomizer.CustomizeEntity(entity, ed, ctx);
                }
            }

            CompilationUnitClass level = GenerateLevel(ldtkJson, ctx);

            fragments.Add(level);

            if (ctx.CodeCustomizer != null)
            {
                ctx.CodeCustomizer.CustomizeLevel(level, ldtkJson, ctx);
            }

            output.OutputCode(fragments, ctx);
        }
        private bool AssignJsonField()
        {
            SerializedProperty jsonProp = serializedObject.FindProperty(LDtkProjectImporter.JSON);

            if (_data != null)
            {
                return(true);
            }

            Object jsonAsset = jsonProp.objectReferenceValue;

            if (jsonAsset == null)
            {
                Debug.LogError("LDtk: Json asset is null, it's never expected to happen. Importer error?");
                return(false);
            }

            LDtkProjectFile jsonFile = (LDtkProjectFile)jsonAsset;
            LdtkJson        json     = jsonFile.FromJson;

            if (json != null)
            {
                _data = jsonFile.FromJson;
                return(true);
            }

            _data = null;
            Debug.LogError("LDtk: Invalid LDtk format");
            jsonProp.objectReferenceValue = null;
            return(false);
        }
        private void DeserializeField(string key, Type type)
        {
            TextAsset fieldAsset = TestJsonLoader.LoadJson($"/LDtkMockField_Project.json");

            //try deserializing field
            LdtkJson field = JsonConvert.DeserializeObject <LdtkJson>(fieldAsset.text);

            FieldInstance[] fieldInstances = field.Levels[0].LayerInstances[0].EntityInstances[0].FieldInstances;

            FieldInstance instance = fieldInstances.First(p => p.Type.Contains(key));



            if (instance.Type.Contains("Array"))
            {
                object[] objs = ((IEnumerable)instance.Value).Cast <object>()
                                .Select(x => x == null ? x : x.ToString())
                                .ToArray();
                foreach (object o in objs)
                {
                    object obj = GetObject(type, instance, o);
                    Debug.Log(obj);
                }
            }
            else
            {
                object obj = GetObject(type, instance, instance.Value);
                Debug.Log(obj);
            }
        }
        public void JsonDeserializeSchemaProject()
        {
            TextAsset jsonProject = TestJsonLoader.LoadJson(BASIC_PROJECT);

            Assert.NotNull(jsonProject, "Unsuccessful acquirement of json text asset");

            //attempt deserializing entire project
            LdtkJson project = LdtkJson.FromJson(jsonProject.text);
        }
Example #5
0
        private bool TryGetJson(out LdtkJson json)
        {
            json = _jsonFile.FromJson;
            if (json != null)
            {
                return(true);
            }

            ImportContext.LogImportError("LDtk: Json import error");
            return(false);
        }
Example #6
0
        private void TryGenerateEnums(LdtkJson json)
        {
            //generate enums
            if (!_enumGenerate || json.Defs.Enums.IsNullOrEmpty())
            {
                return;
            }

            LDtkProjectImporterEnumGenerator enumGenerator = new LDtkProjectImporterEnumGenerator(json.Defs.Enums, ImportContext, _enumPath, _enumNamespace);

            enumGenerator.Generate();
        }
Example #7
0
        public void GetLevelBounds()
        {
            const string lvlName = "Level";

            TextAsset     jsonProject = TestJsonLoader.LoadGenericProject();
            LdtkJson      project     = LdtkJson.FromJson(jsonProject.text);
            Level         level       = project.Levels.FirstOrDefault(p => p.Identifier == lvlName);
            LayerInstance layer       = level.LayerInstances.FirstOrDefault(p => p.IsIntGridLayer);
            Rect          levelBounds = level.UnityWorldSpaceBounds((int)layer.GridSize);

            Debug.Log(levelBounds);
        }
        private LdtkJson GetJson()
        {
            if (_cache == null)
            {
                Debug.LogError("LDtk: Cache was null");
                return(null);
            }

            LdtkJson cachedJson = _cache.Json;

            if (cachedJson != null)
            {
                return(cachedJson);
            }

            return(null);
        }
Example #9
0
    static void Main(string[] args)
    {
        string   text     = File.ReadAllText("Entities.ldtk");
        LdtkJson ldtkJson = LdtkJson.FromJson(text);

        LdtkGeneratorContext ctx = new LdtkGeneratorContext();

        ctx.TypeConverter          = new LdtkTypeConverter();
        ctx.CodeSettings.Namespace = "MyNamespace.Test";

        SingleFileOutput fOut = new SingleFileOutput();

        fOut.OutputDir = "src-gen";
        fOut.Filename  = "MyFileCode.cs";

        LdtkCodeGenerator cg = new LdtkCodeGenerator();

        cg.GenerateCode(ldtkJson, ctx, fOut);
    }
        public void Import(LdtkJson json)
        {
            //set the data class's levels correctly, regardless if they are external levels or not
            json.Levels = GetLevelData(json);


            LDtkProjectBuilder builder = new LDtkProjectBuilder(_importer, json);

            builder.BuildProject();
            GameObject projectGameObject = builder.RootObject;

            if (projectGameObject == null)
            {
                _importer.ImportContext.LogImportError("LDtk: Project GameObject null, not building correctly");
                return;
            }

            _importer.ImportContext.AddObjectToAsset("rootGameObject", projectGameObject, LDtkIconUtility.LoadProjectFileIcon());
            _importer.ImportContext.SetMainObject(projectGameObject);
        }
Example #11
0
        public LDtkJsonEditorCache(LDtkProjectImporter importer)
        {
            _assetPath = importer.assetPath;

            TryCreateKey(_assetPath);

            byte[] newHash = GetFileHash();

            //if the asset is null or check if the new hash is different from the last one, to update the json info for the editor. or if enforced
            if (ShouldDeserialize(newHash))
            {
                LdtkJson fromJson = null;
                try
                {
                    fromJson = importer.JsonFile.FromJson;
                }
                catch
                {
                    // ignored
                }

                GlobalCache[_assetPath] = new Cache()
                {
                    Hash = newHash,
                    Json = fromJson,
                    ShouldReconstruct = false
                };
            }

            if (GlobalCache[_assetPath] == null)
            {
                Debug.LogError("LDtk: A cached editor value is null, this should never be expected");
                return;
            }

            GlobalCache[_assetPath].ShouldReconstruct = false;
            Json = GlobalCache[_assetPath].Json;
        }
        private void ShowGUI()
        {
            EditorGUIUtility.SetIconSize(Vector2.one * 16);

            LdtkJson data = GetJson();

            if (data == null)
            {
                DrawBreakingError();
                return;
            }

            DrawExportButton();
            _sectionMain.SetJson(data);

            Definitions defs = data.Defs;

            _sectionMain.Draw();
            _sectionIntGrids.Draw(defs.IntGridLayers);
            _sectionEntities.Draw(defs.Entities);
            _sectionEnums.Draw(defs.Enums);

            LDtkEditorGUIUtility.DrawDivider();
        }
        /// <summary>
        /// returns the jsons of the level, based on whether we specify external levels.
        /// </summary>
        private Level[] GetLevelData(LdtkJson project)
        {
            if (!project.ExternalLevels)
            {
                return(project.Levels);
            }

            //if we are external levels, we wanna modify the json and serialize it back to that it's usable later with it's completeness regardless of external levels
            Level[] externalLevels = GetExternalLevels(project.Levels);
            project.Levels = externalLevels;

            string newJson = "";

            try
            {
                newJson = project.ToJson();
            }
            finally
            {
                _importer.JsonFile.SetJson(newJson);
            }

            return(project.Levels);
        }
Example #14
0
 public LDtkProjectBuilder(LDtkProjectImporter importer, LdtkJson projectData)
 {
     _importer    = importer;
     _projectData = projectData;
 }
Example #15
0
 public LDtkLevelBuilder(LDtkProjectImporter importer, LdtkJson json, Level level)
 {
     _importer = importer;
     _json     = json;
     _level    = level;
 }
Example #16
0
        private void MainBuild(LdtkJson json)
        {
            LDtkProjectImporterFactory factory = new LDtkProjectImporterFactory(this);

            factory.Import(json);
        }
Example #17
0
 public void SetJson(LdtkJson data)
 {
     _data = data;
 }