示例#1
0
 public void onAssetLoad(AssetLoadEvent @event)
 {
     if (@event.Name == "Minigames\\TitleButtons")
     {
         var path = Path.Combine(PathOnDisk, "TitleButtons.png");
         @event.ReturnValue = @event.Root.LoadResource(path);
         Console.WriteLine(@event.ReturnValue);
     }
 }
示例#2
0
        public static DetourEvent ContentLoadCallback(ContentManager manager, Type assetType, string assetName)
        {
            var @event = new AssetLoadEvent(manager, assetType, assetName);

            HookEvent(@event);
            @event.ReturnValue = EventBus.MapContent(@event);
            //this workaround gives mods priority over manifest resources
            EventBus.Fire(@event);
            //FireEvent(@event);
            return(@event);
        }
示例#3
0
        public void UpdateCallback(AssetLoadEvent @event)
        {
            //This callback actually occurs before InitializeEvent, so we need to load
            // our config here. (Ideally, we would have a super-early fundtion hook that does this.)
            //This is not good design, but it's easy to read.
            if (ModConfig == null)
            {
                //Just dispatch to the actual callback function.
                UpdateCallback(new InitializeEvent());
            }

            //All of our hooks check if the plugin is disabled in config and return early (without touching anything) if that's the case.
            if (!ModConfig.EnablePlugin)
            {
                return;
            }

            //The first thing we hook is the "Maps\springobjects" graphic, a misleadingly-named tileset that contains all inventory-holdable
            // objects. Note that we want "Maps\springobjects", NOT just "springobjects".
            //We use Storm's "LoadResource" function to load a replacement png from our module's folder; this replacement graphic has
            // the Mead and Vodka tiles added in a new row at the bottom of the image.
            //Note that this makes the mod incompatible with most other hacks; ideally, we want some way to add *just* our new row of graphics
            // here, but that currently is not easily doable.
            if (@event.Name == "Maps\\springobjects")
            {
                var path = Path.Combine(PathOnDisk, "springobjects.png");
                @event.ReturnValue = @event.Root.LoadResource(path);
                @event.ReturnEarly = true;
                Console.WriteLine("Overriding resource: " + @event.Name + " with: " + @event.ReturnValue);
            }

            //The second thing we need to over-ride is "Data/ObjectInformation". This YAML dictionary contains a magic string for each
            // ObjectId that describes how much health it restores, how much it sells for, its name, its description, what category
            // that item is bucketed into (in our case, both Mead and Vodka are "drink"), and some other flags related to status that
            // I don't understand yet.
            //We want to return our own dictionary, so I wrote a small function that takes a text file of the form:
            //   ObjectId <Tab> MagicString
            //...and parses that into a dictionary of the appropriate type. It would be fairly easy to parse the original
            // Data/ObjectInformation first, and then insert our lines from ObjectInformation.txt into the dictionary after that,
            // but I'll leave that as an exercise to the reader.
            if (@event.Name == "Data\\ObjectInformation")
            {
                var res = ReadTabbedObjectDictionary(Path.Combine(PathOnDisk, "ObjectInformation.txt"), @event.Name);
                if (res != null)
                {
                    @event.ReturnValue = res;
                    @event.ReturnEarly = true;
                    Console.WriteLine("Overriding resource: " + @event.Name + " with: " + @event.ReturnValue);
                }
            }
        }
示例#4
0
 public object MapContent(AssetLoadEvent @event)
 {
     for (int i = 0; i < mods.Count; i++)
     {
         var lm    = mods[i];
         var props = lm.Properties;
         if (props == null || props.Path == null ||
             props.Resources == null || props.Resources[@event.Name] == null)
         {
             continue;
         }
         var mapped = MapContent(props, @event);
         if (mapped != null)
         {
             return(mapped);
         }
     }
     return(null);
 }
示例#5
0
        private object MapContent(dynamic props, AssetLoadEvent @event)
        {
            var path     = props.Path.ToString();
            var resource = Path.Combine(path, props.Resources[@event.Name].ToString()).ToString();

            if (!File.Exists(resource))
            {
                Console.WriteLine("Missing resource map:" + resource);
                return(null);
            }
            try
            {
                if (@event.Type == typeof(Texture2D))
                {
                    return(@event.Root.LoadResource(resource));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to map:" + @event.Name + " To:" + resource);
                Console.WriteLine(e.ToString());
            }
            return(null);
        }
示例#6
0
    public static void downloadFile(string url, string path, AssetLoadEvent callback, AssetLoadProgress progress = null)
    {
        int    a         = path.LastIndexOf('\\');
        string directory = path.Substring(0, a);

        if (File.Exists(directory) == false)
        {
            Directory.CreateDirectory(directory);
        }
        HttpWebRequest  request  = null;
        HttpWebResponse response = null;

        try
        {
            request  = WebRequest.Create(url) as HttpWebRequest;
            response = request.GetResponse() as HttpWebResponse;
        }
        catch (Exception err)
        {
            Debug.Print("下载文件失败" + err.ToString());
            if (callback != null)
            {
                callback(err.ToString());
            }
            return;
        }

        long   totalBytes          = response.ContentLength;
        Stream responseStream      = response.GetResponseStream();
        Stream stream              = new FileStream(path, FileMode.Create, FileAccess.Write);
        long   totalDownloadedByte = 0;

        try
        {
            byte[] bArr = new byte[1024];
            int    size = responseStream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                totalDownloadedByte = size + totalDownloadedByte;
                stream.Write(bArr, 0, size);
                size = responseStream.Read(bArr, 0, (int)bArr.Length);
                if (progress != null)
                {
                    progress((float)totalDownloadedByte / (float)totalBytes * 100);
                }
                //System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
                //  Thread.Sleep(10);
            }
            stream.Close();
            responseStream.Close();
            //下载成功
            if (callback != null)
            {
                callback(null);
            }
        }
        catch (Exception e)
        {
            Debug.Print("下载文件出错:" + e);
            if (callback != null)
            {
                callback(e.ToString());
            }
        }
        finally {
            stream.Close();
            responseStream.Close();
        }
    }