Пример #1
0
        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;
        }
Пример #2
0
        /// <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);
        }