/// <summary> /// Checks if a certain resource does exist under a certain file system. /// </summary> /// <param name="system"></param> /// <param name="pather"></param> /// <param name="magicChecked"></param> /// <returns></returns> private static bool Exists(IFileSystem system, Pather pather, bool magicChecked) { // If said file exists, good if (system.Exists(pather.RootlessPath)) return true; // If we already tried our magic if (magicChecked) { // Pop the magic again. pather.Pop(); // Second-to-last resort: Extension is .lua, so there might be a luac somewhere. if (pather.Last.EndsWith(".lua") && system.Exists(pather.RootlessPath + "c")) return true; // Last resort: .lua or .luac extension if (system.Exists(pather.RootlessPath + ".lua") || system.Exists(pather.RootlessPath + ".luac")) { string script = pather.Last; pather.Pop(); script += system.Exists(script + ".lua") ? ".lua" : ".luac"; pather.Add(script); return true; } return false; } else { // Try it for an alias if (ParseAlias(pather.ToString()) != null) return true; } // Since we can't check for zip files, try this. // It's not exactly a beautiful approach but it's an allnighter here, so \o/ pather.Add(pather.Last + ".json"); // Just call us again, this time without magic. If we ever redo this part, this would be nice. return Exists(system, pather, true); }
private static Reference resolve(Resource context, string value, bool createIfNonExistant) { if (IsIdentifier(value)) { // Get the folder we are located in (to get some context) Pather p = new Pather(context.Location); p.Pop(); if (Exists(value, p.ToString()) || IsFileReference(value)) { // The resource definitely exists. Resource res; TryGet(value, p.ToString(), out res); Reference reference = context.References.FirstOrDefault(r => r.Target == res); // Create the reference if it didn't already exist if (reference == null && createIfNonExistant) { // Attempt to create an alias reference reference = ParseAlias(value); if (reference != null) context.AddReference(reference); else reference = new Reference(context, context, res); } return reference; } else { // It IS an alias of a mod we *know*? int idx = value.IndexOf(':'); if (idx > 0 && idx < value.Length) { string modName = value.Substring(0, idx); Mod mod; if (mods.TryGetValue(modName, out mod)) { // The mod exists. Alias alias = mod.Aliases.FirstOrDefault(a => a.FullName.Equals(value)); if (alias == null && createIfNonExistant && value.Length > idx + 1) { // Last chance: We're a virtual resource. if (VirtualResources.Contains(value)) alias = new Alias(mod, value.Substring(idx + 1), mod.VirtualResource); else // Nope, missing. alias = new Alias(mod, value.Substring(idx + 1), mod.InvalidResource); } return alias; } } // Is it a file identifier? else if (IsFileReference(value)) { // Well, I guess it's a broken one. Resource res; TryGet(value, p.ToString(), out res); Reference reference = context.References.FirstOrDefault(r => r.Target == res); // Create the reference if it didn't already exist if (reference == null && createIfNonExistant) reference = new Reference(context, context, res); return reference; } } } return null; }
/// <summary> /// Attempts to create a pather to a certain resource and returns the file system and the pather /// </summary> /// <param name="name"></param> /// <param name="context"></param> /// <param name="fs"></param> /// <param name="p"></param> /// <returns></returns> private static bool ConstructPather(string name, string context, out Mod mod, out Pather p) { // Is it a file() path? if (name.StartsWith("file(") && name.EndsWith(")")) { // Our location is relative to our current one. name = name.Substring(5, name.Length - 6); // _unless_ it starts with /. Then it's relative to the mod. if (name[0] == '/') { p = new Pather(context); while (p.Count > 1) p.Pop(); // Skip the /. name = name.Substring(1); } else p = new Pather(context); p.Apply(name); } else // absolute to the mods directory... I guess? p = new Pather(name); // Get the fs if (!mods.TryGetValue(p.Root, out mod)) return false; return true; }