Skip to content

Instantly share code, notes, and snippets.

@yKimisaki
Last active July 30, 2019 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yKimisaki/5a3bc7317e4193c38651b8f8ebff1771 to your computer and use it in GitHub Desktop.
Save yKimisaki/5a3bc7317e4193c38651b8f8ebff1771 to your computer and use it in GitHub Desktop.
/*
MIT License
Copyright (c) 2019 Yoshitaka Kimisaki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
public interface IMasterLoader
{
Task<byte[]> LoadAsync();
}
public static class MasterCache
{
// AirtableMasterLoaderも編集する
public static TestMasterTable TestMaster { get; private set; }
public static async Task RegisterAsync(IMasterLoader loader)
{
var db = new MemoryDatabase(await loader.LoadAsync());
TestMaster = db.TestMasterTable;
}
}
// バイナリのScriptableObjectみたいなの作っておく
public class BinaryObject : ScriptableObject
{
public byte[] Value;
}
// StreamingAssetsにあるバイナリを読み込むやつ
public class ClientMasterLoader : IMasterLoader
{
public static string BinaryAssetBundleName => "masters";
public static string BinaryAssetPath => Path.Combine("Assets", "StreamingResources", "Masters", "Masters.asset");
public async Task<byte[]> LoadAsync()
{
var asset = new StreamingAsset()
{
AssetName = Path.GetFileName(BinaryAssetPath),
AssetBundleName = BinaryAssetBundleName,
AssetFullPath = BinaryAssetPath,
};
// 拡張メソッドでいい感じにしてるけど、各々の方法でAssetBundle読み込む感じでOK
return (await asset.LoadAsync<BinaryObject>()).Value;
}
}
// Airtableから取得してバイナリに変換するやつ
public class AirtableMasterLoader : IMasterLoader
{
public async Task<byte[]> LoadAsync()
{
// 一度エディタ側で登録されているとRegisterで「一度シリアライザで使った後でRegisterするな」
// っていうエラーを出すので、とりあえず握りつぶす
try
{
MessagePack.Resolvers.CompositeResolver.Register(
MessagePack.Resolvers.GeneratedResolver.Instance,
MessagePack.Resolvers.BuiltinResolver.Instance,
MessagePack.Resolvers.PrimitiveObjectResolver.Instance,
MasterMemoryResolver.Instance);
}
catch { }
var builder = new DatabaseBuilder(MessagePack.Resolvers.CompositeResolver.Instance);
using (var client = new AirtableClient("api_key"))
{
builder.Append(await client.GetBase("base_id").LoadTableAsync<TestMaster>());
}
return builder.Build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment