Browse Source

JSONFile resource type

Josh Engebretson 10 years ago
parent
commit
ea6822b89d

+ 1 - 1
Script/AtomicEditor/ui/frames/ResourceFrame.ts

@@ -68,7 +68,7 @@ class ResourceFrame extends ScriptWidget {
 
         var editor: Editor.ResourceEditor = null;
 
-        if (ext == ".js" || ext == ".txt") {
+        if (ext == ".js" || ext == ".txt" || ext == ".json") {
 
             editor = new Editor.JSResourceEditor(path, this.tabcontainer);
 

+ 2 - 1
Script/Packages/ToolCore/ToolCore.json

@@ -7,7 +7,8 @@
 								"Project", "ProjectFile", "Platform", "PlatformMac", "PlatformWeb",
 							 "PlatformWindows", "PlatformAndroid", "PlatformIOS", "Command", "PlayCmd", "OpenAssetImporter",
 							 "Asset", "AssetDatabase", "AssetImporter", "AudioImporter", "ModelImporter", "MaterialImporter", "AnimationImportInfo",
-							 "PrefabImporter", "JavascriptImporter", "TextureImporter", "SpriterImporter", "PEXImporter", "NETAssemblyImporter",
+							 "PrefabImporter", "JavascriptImporter", "JSONImporter",
+							 "TextureImporter", "SpriterImporter", "PEXImporter", "NETAssemblyImporter",
 							 "LicenseSystem",
 						 	 "ProjectUserPrefs", "ProjectBuildSettings",
 						 	 "BuildBase", "BuildSystem", "BuildMac", "BuildWeb", "BuildWindows", "BuildAndroid", "BuildIOS",

+ 5 - 0
Source/ToolCore/Assets/Asset.cpp

@@ -21,6 +21,7 @@
 #include "TextureImporter.h"
 #include "PrefabImporter.h"
 #include "JavascriptImporter.h"
+#include "JSONImporter.h"
 #include "SpriterImporter.h"
 #include "TMXImporter.h"
 #include "PEXImporter.h"
@@ -268,6 +269,10 @@ bool Asset::CreateImporter()
         {
             importer_ = new JavascriptImporter(context_, this);
         }
+        else if (ext == ".json")
+        {
+            importer_ = new JSONImporter(context_, this);
+        }
         else if (ext == ".scene")
         {
             importer_ = new SceneImporter(context_, this);

+ 40 - 0
Source/ToolCore/Assets/AssetDatabase.cpp

@@ -28,6 +28,7 @@ namespace ToolCore
 
 AssetDatabase::AssetDatabase(Context* context) : Object(context)
 {
+    SubscribeToEvent(E_LOADFAILED, HANDLER(AssetDatabase, HandleResourceLoadFailed));
     SubscribeToEvent(E_PROJECTLOADED, HANDLER(AssetDatabase, HandleProjectLoaded));
     SubscribeToEvent(E_PROJECTUNLOADED, HANDLER(AssetDatabase, HandleProjectUnloaded));
 }
@@ -436,11 +437,48 @@ void AssetDatabase::HandleProjectUnloaded(StringHash eventType, VariantMap& even
     cache->RemoveResourceDir(GetCachePath());
     assets_.Clear();
     usedGUID_.Clear();
+    assetImportErrorTimes_.Clear();
     project_ = 0;
 
     UnsubscribeFromEvent(E_FILECHANGED);
 }
 
+void AssetDatabase::HandleResourceLoadFailed(StringHash eventType, VariantMap& eventData)
+{
+
+    if (project_.Null())
+        return;
+
+    String path = eventData[LoadFailed::P_RESOURCENAME].GetString();
+
+    Asset* asset = GetAssetByPath(path);
+
+    if (!asset)
+        asset = GetAssetByPath(project_->GetResourcePath() + path);
+
+    if (!asset)
+        return;
+
+    Time* time = GetSubsystem<Time>();
+
+    unsigned ctime = time->GetSystemTime();
+
+    // if less than 5 seconds since last report, stifle report
+    if (assetImportErrorTimes_.Contains(asset->guid_))
+        if (ctime - assetImportErrorTimes_[asset->guid_] < 5000)
+            return;
+
+    assetImportErrorTimes_[asset->guid_] = ctime;
+
+    VariantMap evData;
+    evData[AssetImportError::P_PATH] = asset->path_;
+    evData[AssetImportError::P_GUID] = asset->guid_;
+    evData[AssetImportError::P_ERROR] = ToString("Asset %s Failed to Load", asset->path_.CString());
+    SendEvent(E_ASSETIMPORTERROR, evData);
+
+
+}
+
 void AssetDatabase::HandleFileChanged(StringHash eventType, VariantMap& eventData)
 {
     using namespace FileChanged;
@@ -496,8 +534,10 @@ String AssetDatabase::GetResourceImporterName(const String& resourceTypeName)
         resourceTypeToImporterType_["Sprite2D"] = "TextureImporter";
         resourceTypeToImporterType_["AnimatedSprite2D"] = "SpriterImporter";
         resourceTypeToImporterType_["JSComponentFile"] = "JavascriptImporter";        
+        resourceTypeToImporterType_["JSONFile"] = "JSONImporter";
         resourceTypeToImporterType_["ParticleEffect2D"] = "PEXImporter";
 
+
 #ifdef ATOMIC_DOTNET
         resourceTypeToImporterType_["CSComponentAssembly"] = "NETAssemblyImporter";
 #endif

+ 4 - 0
Source/ToolCore/Assets/AssetDatabase.h

@@ -55,6 +55,7 @@ private:
     void HandleProjectLoaded(StringHash eventType, VariantMap& eventData);
     void HandleProjectUnloaded(StringHash eventType, VariantMap& eventData);
     void HandleFileChanged(StringHash eventType, VariantMap& eventData);
+    void HandleResourceLoadFailed(StringHash eventType, VariantMap& eventData);
 
     void AddAsset(SharedPtr<Asset>& asset);
 
@@ -70,6 +71,9 @@ private:
 
     HashMap<StringHash, String> resourceTypeToImporterType_;
 
+    /// Hash value of times, so we don't spam import errors
+    HashMap<StringHash, unsigned> assetImportErrorTimes_;
+
     Vector<String> usedGUID_;
 
 };

+ 76 - 0
Source/ToolCore/Assets/JSONImporter.cpp

@@ -0,0 +1,76 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// LICENSE: Atomic Game Engine Editor and Tools EULA
+// Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
+// license information: https://github.com/AtomicGameEngine/AtomicGameEngine
+//
+
+#include <Atomic/IO/Log.h>
+#include <Atomic/IO/File.h>
+#include <Atomic/Resource/ResourceCache.h>
+#include <Atomic/Resource/Image.h>
+
+#include <AtomicJS/Javascript/JSComponentFile.h>
+
+#include "Asset.h"
+#include "AssetDatabase.h"
+#include "JSONImporter.h"
+
+
+namespace ToolCore
+{
+
+JSONImporter::JSONImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
+{
+    requiresCacheFile_ = false;
+}
+
+JSONImporter::~JSONImporter()
+{
+
+}
+
+void JSONImporter::SetDefaults()
+{
+    AssetImporter::SetDefaults();
+}
+
+bool JSONImporter::Import()
+{
+    return true;
+}
+
+bool JSONImporter::LoadSettingsInternal(JSONValue& jsonRoot)
+{
+    if (!AssetImporter::LoadSettingsInternal(jsonRoot))
+        return false;
+
+    JSONValue import = jsonRoot.Get("JSONImporter");
+
+    return true;
+}
+
+bool JSONImporter::SaveSettingsInternal(JSONValue& jsonRoot)
+{
+    if (!AssetImporter::SaveSettingsInternal(jsonRoot))
+        return false;
+
+    JSONValue import;
+    jsonRoot.Set("JSONImporter", import);
+
+    return true;
+}
+
+Resource* JSONImporter::GetResource(const String& typeName)
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+
+    JSONFile* jsfile = cache->GetResource<JSONFile>(asset_->GetPath());
+
+    return jsfile;
+
+}
+
+
+
+}

+ 37 - 0
Source/ToolCore/Assets/JSONImporter.h

@@ -0,0 +1,37 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// LICENSE: Atomic Game Engine Editor and Tools EULA
+// Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
+// license information: https://github.com/AtomicGameEngine/AtomicGameEngine
+//
+
+#pragma once
+
+#include "AssetImporter.h"
+
+namespace ToolCore
+{
+
+class JSONImporter : public AssetImporter
+{
+    OBJECT(JSONImporter);
+
+public:
+    /// Construct.
+    JSONImporter(Context* context, Asset* asset);
+    virtual ~JSONImporter();
+
+    virtual void SetDefaults();
+
+    Resource* GetResource(const String& typeName = String::EMPTY);
+
+protected:
+
+    bool Import();
+
+    virtual bool LoadSettingsInternal(JSONValue& jsonRoot);
+    virtual bool SaveSettingsInternal(JSONValue& jsonRoot);
+
+};
+
+}