Browse Source

Replace single image query with multi image query

main
ThePerkinrex 4 years ago
parent
commit
205599e187
No known key found for this signature in database GPG Key ID: FD81DE6D75E20917
  1. 26
      unity/Assets/Scripts/Client.cs
  2. 433
      unity/Assets/Scripts/GameLoader.cs
  3. 32
      unity/Assets/Scripts/Images.cs

26
unity/Assets/Scripts/Client.cs

@ -11,6 +11,7 @@ using Empty = Google.Protobuf.WellKnownTypes.Empty;
using System.IO; using System.IO;
public class Client : MonoBehaviour { public class Client : MonoBehaviour {
public static DateTime UnixEpoch() => new DateTime(1970, 1, 1, 0, 0, 0, 0);
private static ConnectionImpl reference; private static ConnectionImpl reference;
public static void Connect(string name, string address = "127.0.0.1:50052") { public static void Connect(string name, string address = "127.0.0.1:50052") {
@ -41,6 +42,21 @@ public class Client : MonoBehaviour {
} }
} }
public class Card {
public DateTime timestamp;
public string card;
public Card(string card, DateTime lastUpdated) {
this.card = card;
this.timestamp = lastUpdated > UnixEpoch() ? lastUpdated : UnixEpoch();
}
public Card(string card) {
this.card = card;
this.timestamp = UnixEpoch();
}
}
public class NetEventManager { public class NetEventManager {
private Dictionary<string, Action<Common.Name>> returnNameHandlers = new Dictionary<string, Action<Common.Name>>(); private Dictionary<string, Action<Common.Name>> returnNameHandlers = new Dictionary<string, Action<Common.Name>>();
private Dictionary<string, Action<Connection.UserID>> returnConnectHandlers = new Dictionary<string, Action<Connection.UserID>>(); private Dictionary<string, Action<Connection.UserID>> returnConnectHandlers = new Dictionary<string, Action<Connection.UserID>>();
@ -134,13 +150,15 @@ public class Client : MonoBehaviour {
this.currentImagesPacketLength = (int)p.ReturnCardsImages.Setup.Number; this.currentImagesPacketLength = (int)p.ReturnCardsImages.Setup.Number;
this.currentImagesPacketIndex = 0; this.currentImagesPacketIndex = 0;
this.currentImages = new MemoryStream(); this.currentImages = new MemoryStream();
} else { } else {
if (p.ReturnCardsImages.DataPacket.Id != this.currentImagesPacketIndex) if (p.ReturnCardsImages.DataPacket.Id != this.currentImagesPacketIndex)
throw new Exception("images packet id doesn't match expected"); throw new Exception("images packet id doesn't match expected");
this.currentImages.Write(p.ReturnCardsImages.DataPacket.Data.ToArray(), 0, p.ReturnCardsImages.DataPacket.Data.Length); this.currentImages.Write(p.ReturnCardsImages.DataPacket.Data.Span.ToArray(), 0, p.ReturnCardsImages.DataPacket.Data.Span.Length);
Debug.Log(this.currentImages.ToArray()[0] + ":" + p.ReturnCardsImages.DataPacket.Data.Span.ToArray()[0]);
this.currentImagesPacketIndex++; this.currentImagesPacketIndex++;
if (this.currentImagesPacketIndex == this.currentImagesPacketLength) { if (this.currentImagesPacketIndex == this.currentImagesPacketLength) {
// Debug.Log("Images tar length: " + this.currentImages.Length);
this.currentImages.Seek(0, SeekOrigin.Begin);
Images i = new Images(this.currentImages); Images i = new Images(this.currentImages);
this.currentImagesPacketIndex = 0; this.currentImagesPacketIndex = 0;
var v = new Action<Images>[this.returnCardImagesHandlers.Count]; var v = new Action<Images>[this.returnCardImagesHandlers.Count];
@ -374,9 +392,9 @@ public class Client : MonoBehaviour {
conn.SendMessage(new Protocol.ClientServerPacket() { QueryCardImage = new Game.CardKind { Kind = cardKind } }); conn.SendMessage(new Protocol.ClientServerPacket() { QueryCardImage = new Game.CardKind { Kind = cardKind } });
} }
public void GetCardImages(string cardKind) { public void GetCardImages(Card[] cards) {
Game.Cards c = new Game.Cards(); Game.Cards c = new Game.Cards();
c.Cards_.Append(new Game.Cards.Types.Card { Kind = cardKind, Time = new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = 0, Nanos = 0 } }); c.Cards_.Add(cards.Select(c => new Game.Cards.Types.Card() { Kind = c.card, Time = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(c.timestamp.ToUniversalTime()) }));
conn.SendMessage(new Protocol.ClientServerPacket() { QueryCardImages = c }); conn.SendMessage(new Protocol.ClientServerPacket() { QueryCardImages = c });
} }

433
unity/Assets/Scripts/GameLoader.cs

@ -7,214 +7,239 @@ using UnityEngine.UI;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
public class PileProperties { public class PileProperties {
public string name; public string name;
public string owner; public string owner;
public Game.GameStatus.Types.Card[] cards; public Game.GameStatus.Types.Card[] cards;
public bool faceDown; public bool faceDown;
public GameObject tab; public GameObject tab;
public GameObject gameObject; public GameObject gameObject;
} }
public class CardProperties { public class CardProperties {
public Quaternion rotation; public Quaternion rotation;
public string pileName; public string pileName;
public int idx; public int idx;
public string kind; public string kind;
public GameObject gameObject; public GameObject gameObject;
} }
public class GameLoader : MonoBehaviour { public class GameLoader : MonoBehaviour {
public GameObject table; public GameObject table;
public GameObject playerCube; public GameObject playerCube;
public int myIndex; public int myIndex;
public int playerCount; public int playerCount;
public Color[] playerColors; public Color[] playerColors;
public GameObject playerPiles; public GameObject playerPiles;
public GameObject playerPilePrefab; public GameObject playerPilePrefab;
public GameObject pileTabs; public GameObject pileTabs;
public GameObject pileTabPrefab; public GameObject pileTabPrefab;
public GameObject cardPrefab; public GameObject cardPrefab;
public GameObject cardCanvas; public GameObject cardCanvas;
public GameObject deck; public GameObject deck;
public GameObject thrownCards; public GameObject thrownCards;
public bool firstReload = true; public bool firstReload = true;
private static float yOffsetTable = 0.51f / 2; private static float yOffsetTable = 0.51f / 2;
public static MainMenuController mmc; public static MainMenuController mmc;
public static GameLoader instance; public static GameLoader instance;
public Dictionary<string, Texture2D[]> cardImages = new Dictionary<string, Texture2D[]>(); public Dictionary<string, Texture2D[]> cardImages = new Dictionary<string, Texture2D[]>();
public static Dictionary<string, CardProperties> cardRegistry = new Dictionary<string, CardProperties>(); public static Dictionary<string, CardProperties> cardRegistry = new Dictionary<string, CardProperties>();
public static Dictionary<string, PileProperties> pileRegistry = new Dictionary<string, PileProperties>(); public static Dictionary<string, PileProperties> pileRegistry = new Dictionary<string, PileProperties>();
private Client.ConnectionImpl conn; private Client.ConnectionImpl conn;
void Awake() { void Awake() {
instance = this; instance = this;
} }
void Start() { void Start() {
conn = Client.GetConnection(); conn = Client.GetConnection();
mmc = FindObjectOfType<MainMenuController>(); mmc = FindObjectOfType<MainMenuController>();
mmc.gameObject.SetActive(false); mmc.gameObject.SetActive(false);
playerCount = mmc.usersInLobby.Count; playerCount = mmc.usersInLobby.Count;
if (playerCount > 1) { if (playerCount > 1) {
var angleDelta = 180f / (playerCount - 1); var angleDelta = 180f / (playerCount - 1);
var dst = 15; var dst = 15;
var angleOffset = angleDelta / (2f / (playerCount - 2)); var angleOffset = angleDelta / (2f / (playerCount - 2));
for (int i = 1; i < playerCount; i++) { for (int i = 1; i < playerCount; i++) {
var angle = angleOffset + angleDelta * i; var angle = angleOffset + angleDelta * i;
Vector3 pos = new Vector3(dst * Mathf.Sin(angle * Mathf.Deg2Rad), 2.6f, -dst * Mathf.Cos(angle * Mathf.Deg2Rad)); Vector3 pos = new Vector3(dst * Mathf.Sin(angle * Mathf.Deg2Rad), 2.6f, -dst * Mathf.Cos(angle * Mathf.Deg2Rad));
var player = Instantiate(playerCube, pos, Quaternion.AngleAxis(-angle, Vector3.up)); var player = Instantiate(playerCube, pos, Quaternion.AngleAxis(-angle, Vector3.up));
player.GetComponent<Renderer>().material.color = playerColors[(i + myIndex) % playerCount]; player.GetComponent<Renderer>().material.color = playerColors[(i + myIndex) % playerCount];
} }
} }
} }
public static void RegisterCard(string uuid, CardProperties properties) { public static void RegisterCard(string uuid, CardProperties properties) {
if (!cardRegistry.ContainsKey(uuid)) cardRegistry.Add(uuid, properties); if (!cardRegistry.ContainsKey(uuid)) cardRegistry.Add(uuid, properties);
} }
public static void UpdateCard(string uuid, CardProperties properties) { public static void UpdateCard(string uuid, CardProperties properties) {
if (cardRegistry.ContainsKey(uuid)) cardRegistry[uuid] = properties; if (cardRegistry.ContainsKey(uuid)) cardRegistry[uuid] = properties;
} }
public static CardProperties GetCard(string uuid) { public static CardProperties GetCard(string uuid) {
if (cardRegistry.ContainsKey(uuid)) return cardRegistry[uuid]; if (cardRegistry.ContainsKey(uuid)) return cardRegistry[uuid];
else return null; else return null;
} }
public static void DeleteCard(string uuid) { public static void DeleteCard(string uuid) {
if (cardRegistry.ContainsKey(uuid)) cardRegistry.Remove(uuid); if (cardRegistry.ContainsKey(uuid)) cardRegistry.Remove(uuid);
} }
public static void RegisterPile(string name, PileProperties properties) { public static void RegisterPile(string name, PileProperties properties) {
if (!pileRegistry.ContainsKey(name)) pileRegistry.Add(name, properties); if (!pileRegistry.ContainsKey(name)) pileRegistry.Add(name, properties);
} }
public static void UpdatePile(string name, PileProperties properties) { public static void UpdatePile(string name, PileProperties properties) {
if (pileRegistry.ContainsKey(name)) pileRegistry[name] = properties; if (pileRegistry.ContainsKey(name)) pileRegistry[name] = properties;
} }
public static PileProperties GetPile(string name) { public static PileProperties GetPile(string name) {
if (pileRegistry.ContainsKey(name)) return pileRegistry[name]; if (pileRegistry.ContainsKey(name)) return pileRegistry[name];
else return null; else return null;
} }
public static void DeletePile(string name) { public static void DeletePile(string name) {
if (pileRegistry.ContainsKey(name)) pileRegistry.Remove(name); if (pileRegistry.ContainsKey(name)) pileRegistry.Remove(name);
} }
bool SpawnCard(string uuid, string cardKind, int index, string pileName, bool visible, Game.Image image) { bool SpawnCard(string uuid, string cardKind, int index, string pileName, bool visible, Game.Image image) {
if (image != null && cardKind != image.Kind) return false; if (image != null && cardKind != image.Kind) return false;
PileProperties pileProps = GetPile(pileName); PileProperties pileProps = GetPile(pileName);
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, pileProps.gameObject.transform); var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, pileProps.gameObject.transform);
var front = new Texture2D(1, 1); var front = new Texture2D(1, 1);
var back = new Texture2D(1, 1); var back = new Texture2D(1, 1);
if (cardImages.ContainsKey(cardKind)) { if (cardImages.ContainsKey(cardKind)) {
front = cardImages[cardKind][0]; front = cardImages[cardKind][0];
back = cardImages[cardKind][1]; back = cardImages[cardKind][1];
} else { } else {
front.LoadImage(image.Face.Span.ToArray()); front.LoadImage(image.Face.Span.ToArray());
back.LoadImage(image.Back.Span.ToArray()); back.LoadImage(image.Back.Span.ToArray());
cardImages.Add(cardKind, new Texture2D[] { front, back }); cardImages.Add(cardKind, new Texture2D[] { front, back });
} }
card.transform.Find("Images").GetComponentsInChildren<RawImage>()[0].texture = front; card.transform.Find("Images").GetComponentsInChildren<RawImage>()[0].texture = front;
card.transform.Find("Images").GetComponentsInChildren<RawImage>()[1].texture = back; card.transform.Find("Images").GetComponentsInChildren<RawImage>()[1].texture = back;
card.transform.localScale = new Vector3(0.15f, 0.15f, 1f); card.transform.localScale = new Vector3(0.15f, 0.15f, 1f);
card.GetComponent<Card>().uuid = uuid; card.GetComponent<Card>().uuid = uuid;
CardProperties cardProps = GetCard(uuid); CardProperties cardProps = GetCard(uuid);
GameObject canvas = null; GameObject canvas = null;
if (pileProps.owner == "") { if (pileProps.owner == "") {
canvas = Instantiate(cardCanvas, pileProps.gameObject.transform.position + new Vector3(0, yOffsetTable + pileProps.gameObject.transform.childCount * 0.0075f, 0), cardProps != null ? cardProps.rotation : Quaternion.identity); canvas = Instantiate(cardCanvas, pileProps.gameObject.transform.position + new Vector3(0, yOffsetTable + pileProps.gameObject.transform.childCount * 0.0075f, 0), cardProps != null ? cardProps.rotation : Quaternion.identity);
canvas.GetComponent<Canvas>().worldCamera = Camera.main; canvas.GetComponent<Canvas>().worldCamera = Camera.main;
card.transform.localScale = Vector3.one; card.transform.localScale = Vector3.one;
card.transform.SetParent(canvas.transform, false); card.transform.SetParent(canvas.transform, false);
canvas.transform.SetParent(pileProps.gameObject.transform); canvas.transform.SetParent(pileProps.gameObject.transform);
canvas.tag = "ThrownCard"; canvas.tag = "ThrownCard";
canvas.transform.localScale = new Vector3(0.01f, 0.01f); canvas.transform.localScale = new Vector3(0.01f, 0.01f);
if (canvas.transform.rotation == Quaternion.identity) { if (canvas.transform.rotation == Quaternion.identity) {
canvas.transform.Rotate(new Vector3(90f, 0f, 0f)); canvas.transform.Rotate(new Vector3(90f, 0f, 0f));
canvas.transform.Rotate(new Vector3(0f, 0f, Random.Range(-20, 20))); canvas.transform.Rotate(new Vector3(0f, 0f, Random.Range(-20, 20)));
} }
} }
if(!visible || pileProps.faceDown) { if (!visible || pileProps.faceDown) {
card.transform.GetChild(1).Rotate(0, 180f, 0); card.transform.GetChild(1).Rotate(0, 180f, 0);
} }
if (cardProps != null) { if (cardProps != null) {
if (cardProps.kind != cardKind) cardProps.kind = cardKind; if (cardProps.kind != cardKind) cardProps.kind = cardKind;
if (cardProps.idx != index) cardProps.idx = index; if (cardProps.idx != index) cardProps.idx = index;
if (cardProps.pileName != pileName) cardProps.pileName = pileName; if (cardProps.pileName != pileName) cardProps.pileName = pileName;
if (cardProps.gameObject != card) cardProps.gameObject = card; if (cardProps.gameObject != card) cardProps.gameObject = card;
if (canvas != null && cardProps.rotation != canvas.transform.rotation) cardProps.rotation = canvas.transform.rotation; if (canvas != null && cardProps.rotation != canvas.transform.rotation) cardProps.rotation = canvas.transform.rotation;
UpdateCard(uuid, cardProps); UpdateCard(uuid, cardProps);
} else { } else {
cardProps = new CardProperties { kind = cardKind, idx = index, pileName = pileName, rotation = canvas != null ? canvas.transform.rotation : Quaternion.identity, gameObject = card }; cardProps = new CardProperties { kind = cardKind, idx = index, pileName = pileName, rotation = canvas != null ? canvas.transform.rotation : Quaternion.identity, gameObject = card };
RegisterCard(uuid, cardProps); RegisterCard(uuid, cardProps);
} }
return true; return true;
} }
void SpawnCards() { void SpawnCards() {
foreach (string key in pileRegistry.Keys) { List<Client.Card> cards = new List<Client.Card>();
int idx = 0; foreach (string key in pileRegistry.Keys) {
foreach (Game.GameStatus.Types.Card card in pileRegistry[key].cards) { foreach (Game.GameStatus.Types.Card card in pileRegistry[key].cards) {
if (cardImages.ContainsKey(card.Kind.Kind)) { if (!cardImages.ContainsKey(card.Kind.Kind)) {
SpawnCard(card.Uuid, card.Kind.Kind, idx, key, card.Visible, null); // string uuid = card.Uuid;
} else { // string kind = card.Kind.Kind;
string uuid = card.Uuid; // int idx2 = idx;
string kind = card.Kind.Kind; // string key2 = key;
int idx2 = idx; // bool visible = card.Visible;
string key2 = key; // conn.eventManager.AddHandler("game_card_image_" + uuid, (Game.Image image) => {
bool visible = card.Visible; // if(!SpawnCard(uuid, kind, idx2, key2, visible, image)) return;
conn.eventManager.AddHandler("game_card_image_" + uuid, (Game.Image image) => { // conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_" + uuid);
if(!SpawnCard(uuid, kind, idx2, key2, visible, image)) return; // });
conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_" + uuid); // conn.GetCardImage(card.Kind.Kind);
}); cards.Add(new Client.Card(card.Kind.Kind));
conn.GetCardImage(card.Kind.Kind); }
} }
idx++; }
} string handlerName = "game_cards_images_" + (DateTime.Now - Client.UnixEpoch()).TotalMilliseconds;
} conn.eventManager.AddHandler(handlerName, (Images i) => {
} foreach (string key in pileRegistry.Keys) {
public static void ReloadPiles() { int idx = 0;
if (instance == null) return; foreach (Game.GameStatus.Types.Card card in pileRegistry[key].cards) {
var piles = instance.conn.GetPlayerPiles(mmc.currentUsername); if (cardImages.ContainsKey(card.Kind.Kind)) {
SpawnCard(card.Uuid, card.Kind.Kind, idx, key, card.Visible, null);
foreach (var pair in piles.Keys.Zip(piles.Values, (key, value) => new dynamic[] { key, value })) { } else {
string key = (string)pair[0]; string uuid = card.Uuid;
Game.GameStatus.Types.Pile value = (Game.GameStatus.Types.Pile)pair[1]; string kind = card.Kind.Kind;
int idx2 = idx;
var pileEntry = GetPile(key + mmc.currentUsername); string key2 = key;
if (pileEntry == null) { bool visible = card.Visible;
var pile = Instantiate(instance.playerPilePrefab, instance.playerPiles.transform); // conn.eventManager.AddHandler("game_card_image_" + uuid, (Game.Image image) => {
pile = pile.transform.GetChild(0).gameObject; // if (!SpawnCard(uuid, kind, idx2, key2, visible, image)) return;
// conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_" + uuid);
var tab = Instantiate(instance.pileTabPrefab, instance.pileTabs.transform); // });
tab.GetComponentInChildren<Text>().text = value.Name; // conn.GetCardImage(card.Kind.Kind);
Game.Image image = i.GetImage(kind);
RegisterPile(key + mmc.currentUsername, new PileProperties { name = key, owner = mmc.currentUsername, cards = value.Cards.ToArray(), gameObject = pile, tab = tab, faceDown = value.FaceDown }); SpawnCard(uuid, kind, idx2, key2, visible, image);
} else UpdatePile(key + mmc.currentUsername, new PileProperties { name = key, owner = mmc.currentUsername, cards = value.Cards.ToArray(), gameObject = pileEntry.gameObject, tab = pileEntry.tab, faceDown = value.FaceDown }); }
idx++;
pileEntry = GetPile(key + mmc.currentUsername); }
pileEntry.tab.SetActive(value.Visible); }
pileEntry.gameObject.SetActive(value.Visible); conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardsImages, handlerName);
} });
if((instance.firstReload && instance.pileTabs.GetComponentInChildren<Button>() != null) || instance.pileTabs.GetComponentsInChildren<Button>().Length == 1) instance.pileTabs.GetComponentInChildren<Button>().onClick.Invoke(); conn.GetCardImages(cards.ToArray());
}
// Hardcoded values for pile gameobjects public static void ReloadPiles() {
piles = instance.conn.GetCommonPiles(); if (instance == null) return;
foreach (var pair in piles.Keys.Zip(piles.Values, (key, value) => new dynamic[] { key, value })) { var piles = instance.conn.GetPlayerPiles(mmc.currentUsername);
string key = (string)pair[0];
Game.GameStatus.Types.Pile value = (Game.GameStatus.Types.Pile)pair[1]; foreach (var pair in piles.Keys.Zip(piles.Values, (key, value) => new dynamic[] { key, value })) {
if (GetPile(key + "_common") == null) RegisterPile(key + "_common", new PileProperties { name = key, owner = "", cards = value.Cards.ToArray(), gameObject = key == "placed" ? instance.thrownCards : instance.deck, faceDown = value.FaceDown }); string key = (string)pair[0];
else UpdatePile(key+"_common", new PileProperties { name = key, owner = "", cards = value.Cards.ToArray(), gameObject = key == "placed" ? instance.thrownCards : instance.deck, faceDown = value.FaceDown }); Game.GameStatus.Types.Pile value = (Game.GameStatus.Types.Pile)pair[1];
}
var cards = FindObjectsOfType<Card>(true); var pileEntry = GetPile(key + mmc.currentUsername);
foreach (Card card in cards) { if (pileEntry == null) {
if (card.IsThrown()) Destroy(card.transform.parent.gameObject); var pile = Instantiate(instance.playerPilePrefab, instance.playerPiles.transform);
else Destroy(card.gameObject); pile = pile.transform.GetChild(0).gameObject;
}
var tab = Instantiate(instance.pileTabPrefab, instance.pileTabs.transform);
instance.SpawnCards(); tab.GetComponentInChildren<Text>().text = value.Name;
instance.firstReload = false;
} RegisterPile(key + mmc.currentUsername, new PileProperties { name = key, owner = mmc.currentUsername, cards = value.Cards.ToArray(), gameObject = pile, tab = tab, faceDown = value.FaceDown });
} else UpdatePile(key + mmc.currentUsername, new PileProperties { name = key, owner = mmc.currentUsername, cards = value.Cards.ToArray(), gameObject = pileEntry.gameObject, tab = pileEntry.tab, faceDown = value.FaceDown });
pileEntry = GetPile(key + mmc.currentUsername);
pileEntry.tab.SetActive(value.Visible);
pileEntry.gameObject.SetActive(value.Visible);
}
if ((instance.firstReload && instance.pileTabs.GetComponentInChildren<Button>() != null) || instance.pileTabs.GetComponentsInChildren<Button>().Length == 1) instance.pileTabs.GetComponentInChildren<Button>().onClick.Invoke();
// Hardcoded values for pile gameobjects
piles = instance.conn.GetCommonPiles();
foreach (var pair in piles.Keys.Zip(piles.Values, (key, value) => new dynamic[] { key, value })) {
string key = (string)pair[0];
Game.GameStatus.Types.Pile value = (Game.GameStatus.Types.Pile)pair[1];
if (GetPile(key + "_common") == null) RegisterPile(key + "_common", new PileProperties { name = key, owner = "", cards = value.Cards.ToArray(), gameObject = key == "placed" ? instance.thrownCards : instance.deck, faceDown = value.FaceDown });
else UpdatePile(key + "_common", new PileProperties { name = key, owner = "", cards = value.Cards.ToArray(), gameObject = key == "placed" ? instance.thrownCards : instance.deck, faceDown = value.FaceDown });
}
var cards = FindObjectsOfType<Card>(true);
foreach (Card card in cards) {
if (card.IsThrown()) Destroy(card.transform.parent.gameObject);
else Destroy(card.gameObject);
}
instance.SpawnCards();
instance.firstReload = false;
}
} }

32
unity/Assets/Scripts/Images.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Text; using System.Text;
using UnityEngine;
public class Images { public class Images {
private List<byte[]> images = new List<byte[]>(); private List<byte[]> images = new List<byte[]>();
@ -16,16 +17,25 @@ public class Images {
return new DeflateStream(s, CompressionMode.Decompress); return new DeflateStream(s, CompressionMode.Decompress);
} }
private void ExtractTar(Stream stream) { private void ExtractTar(Stream maybeSeekableStream) {
Stream stream;
if (maybeSeekableStream.CanSeek) {
stream = maybeSeekableStream;
}else{
stream = new MemoryStream();
maybeSeekableStream.CopyTo(stream);
stream.Seek(0, SeekOrigin.Begin);
}
var buffer = new byte[100]; var buffer = new byte[100];
while (true) { while (true) {
stream.Read(buffer, 0, 100); stream.Read(buffer, 0, 100);
var name = Encoding.ASCII.GetString(buffer).Trim('\0'); var name = Encoding.ASCII.GetString(buffer).Trim('\0');
// Debug.Log("Unpacked image: " + name);
if (String.IsNullOrWhiteSpace(name)) if (String.IsNullOrWhiteSpace(name))
break; break;
stream.Seek(24, SeekOrigin.Current); stream.Seek(24, SeekOrigin.Current);
stream.Read(buffer, 0, 12); stream.Read(buffer, 0, 12);
var size = Convert.ToInt64(Encoding.ASCII.GetString(buffer, 0, 12).Trim(), 8); var size = Convert.ToInt64(Encoding.ASCII.GetString(buffer, 0, 12).Trim().Trim('\0'), 8);
stream.Seek(376L, SeekOrigin.Current); stream.Seek(376L, SeekOrigin.Current);
@ -33,8 +43,9 @@ public class Images {
stream.Read(buf, 0, buf.Length); stream.Read(buf, 0, buf.Length);
if (name.EndsWith(".ref.txt")) { if (name.EndsWith(".ref.txt")) {
string r = Encoding.UTF8.GetString(buf).Trim('\0'); string r = Encoding.UTF8.GetString(buf).Trim('\0');
refs.Add(name.Replace(".ref.txt", ".png"), refs[r]); if (!refs.ContainsKey(name.Replace(".ref.txt", ".png")))
}else{ refs.Add(name.Replace(".ref.txt", ".png"), refs[r]);
} else {
int i = images.Count; int i = images.Count;
images.Add(buf); images.Add(buf);
refs.Add(name, i); refs.Add(name, i);
@ -50,8 +61,17 @@ public class Images {
} }
} }
public byte[] GetImage(string name) { public byte[] GetImageFromPath(string path) {
return images[refs[name]]; // Debug.Log("Getting image: " + path);
return images[refs[path]];
}
public Game.Image GetImage(string cardKind) {
return new Game.Image() {
Kind = cardKind,
Face = Google.Protobuf.ByteString.CopyFrom(GetImageFromPath(cardKind + "/face.png")),
Back = Google.Protobuf.ByteString.CopyFrom(GetImageFromPath(cardKind + "/back.png"))
};
} }
} }
Loading…
Cancel
Save