/// <summary> /// Tries to fetch said entry. If it does not exist, creates it. /// </summary> /// <param name="name"></param> /// <param name="result"></param> /// <returns></returns> public static bool TryGet(string name, string context, out Resource result) { Mod mod; Pather p; // Is it an alias? Alias al = ParseAlias(name); if (al != null) { result = al.Target; return true; } // If we can't construct a pather then all is lost if (!ConstructPather(name, context, out mod, out p)) { result = null; return false; } // The... uh... uhhhhm.... // The... resolved path. Yes. That sounds good enough. var fullPath = p.ToString(); // Try to fetch the resource from our list? if (Resources.TryGetValue(fullPath, out result)) return true; // Checks if the path exists + adjusts our pather bool exists = Exists(mod.FileSystem, p, false); fullPath = p.ToString(); // Try again with our new path? if (Resources.TryGetValue(fullPath, out result)) return true; // Create a new resource then. string location = p.ToString(); if (Path.GetExtension(location) == ".json") result = new JsonResource(location, mod, exists); else result = new Resource(location, mod, exists); Resources.Add(location, result); if (exists && result is JsonResource) Queue.Enqueue(result); return exists; }
/// <summary> /// Used in <see cref="ResolveJson"/>. /// </summary> /// <param name="resource"></param> /// <param name="token"></param> private static void translate(JsonResource resource, JToken token) { if (token.Type != JTokenType.String) return; JValue jv = (JValue)token; Reference reference = resolve(resource, jv.ToString(), false); if (reference == null) return; jv.Value = reference.Target.Location; }
/// <summary> /// Resolves a JSON Resource's *original* token. /// </summary> /// <param name="token"></param> public static JToken ResolveJson(JsonResource resource) { JToken root = resource.OriginalToken.DeepClone(); var walker = new Providers.JsonWalker(root); List<JToken> keyTokens = new List<JToken>(); walker.OnToken += t => translate(resource, t); //walker.OnKey += t => { var r = resolve(resource, t.Name, false); }; walker.Work(); return root; }