Giter Club home page Giter Club logo

Comments (9)

davyxu avatar davyxu commented on August 10, 2024

没看懂你这个写法。
读取表后,是需要自己做数据处理。

from tabtoy.

VergilGao avatar VergilGao commented on August 10, 2024

不好意思我之前没写清楚思路,这里有一个按照我思路大概写出来的一个读表类,可以做到对所有的表都可以直接读取,而不需要重复写读表这个过程:

using tabtoy;

public class TabtoyHelper<TConfig, TDefine> where TConfig : ITabToyConfig, TDefine : IDefine {

    public TConfig DataReader (string filePath) {
        using (var stream = new FileStream (filePath, FileMode.Open)) {
            stream.Position = 0;

            var reader = new DataReader (stream);

            if (!reader.ReadHeader ()) {
                Console.WriteLine ("combine file crack!");
                return;
            }

            var config = new TConfig ();
            TConfig.Deserialize (config, reader);

            return config;
        }
    }
}

这个类可以由使用者自己来写,根据他们的需要用DI或者单例等方式来写这个TabtoyHelper

要实现这样的写法只需要在生成的C#文件中,Config类继承一个ITabToyConfig的空接口,PlayerDefine类继承一个IDefine的空接口。同时在原本tabtoy的C#代码中加入上述两个空接口的定义即可。

from tabtoy.

davyxu avatar davyxu commented on August 10, 2024

你是想把每个结构单独读取吧?

from tabtoy.

VergilGao avatar VergilGao commented on August 10, 2024

不是,假如我有5个不同的表,Player, Item, Map 。。。
我通过上面写的方法就可以做到一套代码复用给这5个表的读取,至于读取完数据怎么用,再分别写各自的代码。
因为我发现上面的代码我每处理一个新的表,都要复制改写一次。

比如我想读取Player,就直接

var playerConfig = TabtoyHelper<Player.Config, PlayerDefine>.instance.DataReader("Data/Player.bin");
var player0 = playerConfig.GetPlayerByID(0);

我相当于把读取部分的代码复用了。

from tabtoy.

davyxu avatar davyxu commented on August 10, 2024

你不是直接用表数据,而是拷贝一份吧?

from tabtoy.

VergilGao avatar VergilGao commented on August 10, 2024

对的,我这里就是把表数据缓存下来了,然后按需使用。
如果我把TabtoyHelper做成单例类的话,也可以通过Clear方法清理缓存。
总的来说,就是对数据的使用更自由了。

from tabtoy.

davyxu avatar davyxu commented on August 10, 2024

这种并不是tabtoy建议的用法,两份数据你维护起来会很麻烦。

from tabtoy.

VergilGao avatar VergilGao commented on August 10, 2024

我没有两份数据啊,只有一份数据。

又或者,我没有缓存,我把上面的代码写成静态类:

public static class TabtoyHelper<TConfig, TDefine> where TConfig : ITabToyConfig, TDefine : IDefine {

    public static TConfig DataReader (string filePath) {
        using (var stream = new FileStream (filePath, FileMode.Open)) {
            stream.Position = 0;

            var reader = new DataReader (stream);

            if (!reader.ReadHeader ()) {
                Console.WriteLine ("combine file crack!");
                return;
            }

            var config = new TConfig ();
            TConfig.Deserialize (config, reader);

            return config;
        }
    }
}

至少我做到了对这段代码的复用,而不是每次需要读表的时候都要重复写一次上面的代码。

否则你看一下,如果我有5个表,那么上面的代码我至少要写5次。

from tabtoy.

Wecica avatar Wecica commented on August 10, 2024

不好意思我之前没写清楚思路,这里有一个按照我思路大概写出来的一个读表类,可以做到对所有的表都可以直接读取,而不需要重复写读表这个过程:

using tabtoy;

public class TabtoyHelper<TConfig, TDefine> where TConfig : ITabToyConfig, TDefine : IDefine {

    public TConfig DataReader (string filePath) {
        using (var stream = new FileStream (filePath, FileMode.Open)) {
            stream.Position = 0;

            var reader = new DataReader (stream);

            if (!reader.ReadHeader ()) {
                Console.WriteLine ("combine file crack!");
                return;
            }

            var config = new TConfig ();
            TConfig.Deserialize (config, reader);

            return config;
        }
    }
}

这个类可以由使用者自己来写,根据他们的需要用DI或者单例等方式来写这个TabtoyHelper

要实现这样的写法只需要在生成的C#文件中,Config类继承一个ITabToyConfig的空接口,PlayerDefine类继承一个IDefine的空接口。同时在原本tabtoy的C#代码中加入上述两个空接口的定义即可。

你这里TConfig.Deserialize(config, reader)不能直接调用吧,你这么解决这里的问题的?

from tabtoy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.