Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Data.SQLite;
- namespace d3fileparser
- {
- public class MobAnim
- {
- public List<d3fileparser.Program.Actor> Actors = new List<d3fileparser.Program.Actor>();
- public static void Serialize(string file, MobAnim c)
- {
- if (!File.Exists(file))
- {
- // File.Create(file);
- }
- System.Xml.Serialization.XmlSerializer xs
- = new System.Xml.Serialization.XmlSerializer(c.GetType());
- StreamWriter writer = File.CreateText(file);
- xs.Serialize(writer, c);
- writer.Flush();
- writer.Close();
- }
- public static MobAnim Deserialize(string file)
- {
- System.Xml.Serialization.XmlSerializer xs
- = new System.Xml.Serialization.XmlSerializer(
- typeof(MobAnim));
- StreamReader reader = File.OpenText(file);
- MobAnim c = (MobAnim)xs.Deserialize(reader);
- reader.Close();
- return c;
- }
- }
- public class Program
- {
- public class Actor
- {
- public int ActorSNO;
- public int AnimSetSNO;
- public AnimSet AnimationSet;
- }
- public class AnimSet
- {
- public int AnimsetSNO;
- public int NumAnimations;
- public List<Animation> Animations = new List<Animation>();
- public Animation GetAniByID(int id)
- {
- return Animations.Single(s => s.AniSNO == id);
- }
- }
- public class Animation
- {
- public string name;
- public int AniSNO;
- public int AniTagID;
- }
- static string path = "c:\\d3\\";
- static void Main(string[] args)
- {
- /*Program p = new Program();
- List<Actor> actors = new List<Actor>();
- actors = p.ParseActor();
- List<AnimSet> sofar = p.ParseAnimSet();
- p.ParseAni(sofar);
- for(int i=0; i < actors.Count;i++)
- {
- actors[i].AnimationSet = new AnimSet();
- if (actors[i].AnimSetSNO < 1) { continue; }
- actors[i].AnimationSet = sofar.Single(az => az.AnimsetSNO == actors[i].AnimSetSNO);
- }
- MobAnim mb = new MobAnim();
- mb.Actors = actors;
- MobAnim.Serialize(path + "all.txt", mb);*/
- AnimSet aaaa = GetAnimSetByID(6652);
- MobAnim mb = MobAnim.Deserialize(path + "ActorAnimData.txt");
- SQLiteConnection Connection = new SQLiteConnection("Data Source=animation.db");
- Connection.Open();
- int anims = 0;
- SQLiteTransaction trans;
- trans = Connection.BeginTransaction();
- SQLiteCommand cmd = new SQLiteCommand(Connection);
- cmd.Transaction = trans;
- foreach (Actor a in mb.Actors)
- {
- var query =
- string.Format("INSERT INTO anim (actorsno, animstart, animend) VALUES({0},{1},{2})", a.ActorSNO, anims, anims + a.AnimationSet.NumAnimations);
- cmd.CommandText = query;
- //var cmd = new SQLiteCommand(query, Connection);
- cmd.ExecuteNonQuery();
- foreach(Animation ani in a.AnimationSet.Animations)
- {
- var query2 =
- string.Format("INSERT INTO Animations (animindex, sno, name, tagid) VALUES({0},{1},'{2}',{3})",anims,ani.AniSNO,ani.name,ani.AniTagID);
- //var cmd2 = new SQLiteCommand(query2, Connection);
- cmd.CommandText = query2;
- cmd.ExecuteNonQuery();
- anims++;
- }
- }
- trans.Commit();
- Connection.Close();
- }
- public static AnimSet GetAnimSetByID(int ActorID)
- {
- SQLiteConnection Connection = new SQLiteConnection("Data Source=animation.db");
- Connection.Open();
- SQLiteCommand cmd = new SQLiteCommand(Connection);
- cmd.CommandText = "SELECT * from anim WHERE actorsno=" + ActorID;
- var reader = cmd.ExecuteReader();
- if (!reader.HasRows) return null;
- reader.Read();
- int start = reader.GetInt32(1); int end = reader.GetInt32(2);
- reader.Close();
- cmd.CommandText = "SELECT * from Animations WHERE animindex >" + start + " AND animindex <" + end;
- reader = cmd.ExecuteReader();
- AnimSet anim = new AnimSet();
- while (reader.Read())
- {
- Animation ani = new Animation();
- ani.AniSNO = reader.GetInt32(1);
- ani.name = reader.GetString(2);
- ani.AniTagID = reader.GetInt32(3);
- anim.Animations.Add(ani);
- }
- Connection.Close();
- return anim;
- }
- int ReadInt32(FileStream fs)
- {
- byte[] b = new byte[4];
- int result = fs.Read(b, 0, 4);
- return BitConverter.ToInt32(b, 0);
- }
- List<AnimSet> ParseAnimSet()
- {
- List<AnimSet> list = new List<AnimSet>();
- var fullpath = path + "AnimSet\\";
- foreach (string s in Directory.EnumerateFiles(fullpath))
- {
- AnimSet a = new AnimSet();
- a.Animations = new List<Animation>();
- FileStream fs = File.OpenRead(s);
- fs.Seek(16, SeekOrigin.Begin);
- int AnimSetSNO = ReadInt32(fs);
- a.AnimsetSNO = AnimSetSNO;
- fs.Position = 352;
- int NumAnimations = ReadInt32(fs);
- a.NumAnimations = NumAnimations;
- for (int i = 0; i <= NumAnimations; i++)
- {
- fs.Position += 4;
- int TagID = ReadInt32(fs);
- int AniSNO = ReadInt32(fs);
- Animation ani = new Animation();
- ani.AniSNO = AniSNO;
- ani.AniTagID = TagID;
- a.Animations.Add(ani);
- }
- list.Add(a);
- fs.Close();
- }
- return list;
- }
- List<Actor> ParseActor()
- {
- List<Actor> mylist = new List<Actor>();
- var fullpath = path + "actor\\";
- foreach (string s in Directory.EnumerateFiles(fullpath))
- {
- FileStream fs = File.OpenRead(s);
- fs.Seek(16, SeekOrigin.Begin);
- int ActorSNO = ReadInt32(fs);
- fs.Position = 120;
- int AnimsetSNO = ReadInt32(fs);
- fs.Close();
- Actor a = new Actor();
- a.ActorSNO = ActorSNO;
- a.AnimSetSNO = AnimsetSNO;
- a.AnimationSet = new AnimSet();
- a.AnimationSet.AnimsetSNO = AnimsetSNO;
- mylist.Add(a);
- fs.Close();
- }
- return mylist;
- }
- byte[] bytestillnull(FileStream fs)
- {
- List<byte> list = new List<byte>();
- while (true)
- {
- byte b = (byte)fs.ReadByte();
- if (b == 0) { if (fs.ReadByte() == 0) { return list.ToArray(); } else { fs.Position += -1; } }
- list.Add(b);
- }
- return null;
- }
- void ParseAni(List<AnimSet> list)
- {
- var fullpath = path + "Anim\\";
- foreach (string s in Directory.EnumerateFiles(fullpath))
- {
- FileStream fs = File.OpenRead(s);
- fs.Seek(16, SeekOrigin.Begin);
- int aniSNO = ReadInt32(fs);
- string name = "";
- fs.Position = 76; // WRONG some ani's it starts earlier NEED TO FIX
- name = System.Text.ASCIIEncoding.ASCII.GetString(bytestillnull(fs));
- try { list.Single(a => a.Animations.Exists(az => az.AniSNO == aniSNO)).GetAniByID(aniSNO).name = name;
- Console.WriteLine(name);
- }
- catch { }
- }
- var fa = File.CreateText(path + "all.txt");
- foreach (var pv in list)
- {
- }
- fa.Close();
- }
- }
- }
Add Comment
Please, Sign In to add comment