Guest User

Untitled

a guest
Jan 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Data.SQLite;
  7. namespace d3fileparser
  8. {
  9.     public class MobAnim
  10.     {
  11.         public List<d3fileparser.Program.Actor> Actors = new List<d3fileparser.Program.Actor>();
  12.         public static void Serialize(string file, MobAnim c)
  13.         {
  14.             if (!File.Exists(file))
  15.             {
  16.                 // File.Create(file);
  17.             }
  18.             System.Xml.Serialization.XmlSerializer xs
  19.                = new System.Xml.Serialization.XmlSerializer(c.GetType());
  20.             StreamWriter writer = File.CreateText(file);
  21.             xs.Serialize(writer, c);
  22.             writer.Flush();
  23.             writer.Close();
  24.         }
  25.         public static MobAnim Deserialize(string file)
  26.         {
  27.             System.Xml.Serialization.XmlSerializer xs
  28.                = new System.Xml.Serialization.XmlSerializer(
  29.                   typeof(MobAnim));
  30.             StreamReader reader = File.OpenText(file);
  31.             MobAnim c = (MobAnim)xs.Deserialize(reader);
  32.             reader.Close();
  33.             return c;
  34.         }
  35.  
  36.     }
  37.     public class Program
  38.     {
  39.        
  40.         public class Actor
  41.         {
  42.             public int ActorSNO;
  43.             public int AnimSetSNO;
  44.             public AnimSet AnimationSet;
  45.         }
  46.  
  47.         public class AnimSet
  48.         {
  49.             public int AnimsetSNO;
  50.             public int NumAnimations;
  51.             public List<Animation> Animations = new List<Animation>();
  52.             public Animation GetAniByID(int id)
  53.             {
  54.                 return Animations.Single(s => s.AniSNO == id);
  55.             }
  56.         }
  57.         public class Animation
  58.         {
  59.             public string name;
  60.             public int AniSNO;
  61.             public int AniTagID;
  62.         }
  63.  
  64.         static string path = "c:\\d3\\";
  65.         static void Main(string[] args)
  66.         {
  67.             /*Program p = new Program();
  68.             List<Actor> actors = new List<Actor>();
  69.             actors = p.ParseActor();
  70.             List<AnimSet> sofar = p.ParseAnimSet();
  71.             p.ParseAni(sofar);
  72.             for(int i=0; i < actors.Count;i++)
  73.             {
  74.                 actors[i].AnimationSet = new AnimSet();
  75.                 if (actors[i].AnimSetSNO < 1) { continue; }
  76.                 actors[i].AnimationSet = sofar.Single(az => az.AnimsetSNO == actors[i].AnimSetSNO);
  77.             }
  78.             MobAnim mb = new MobAnim();
  79.             mb.Actors = actors;
  80.             MobAnim.Serialize(path + "all.txt", mb);*/
  81.             AnimSet aaaa = GetAnimSetByID(6652);
  82.             MobAnim mb = MobAnim.Deserialize(path + "ActorAnimData.txt");
  83.             SQLiteConnection Connection = new SQLiteConnection("Data Source=animation.db");
  84.             Connection.Open();
  85.             int anims = 0;
  86.             SQLiteTransaction trans;
  87.             trans = Connection.BeginTransaction();
  88.             SQLiteCommand cmd = new SQLiteCommand(Connection);
  89.             cmd.Transaction = trans;
  90.            
  91.  
  92.             foreach (Actor a in mb.Actors)
  93.             {
  94.                 var query =
  95.                        string.Format("INSERT INTO anim (actorsno, animstart, animend) VALUES({0},{1},{2})", a.ActorSNO, anims, anims + a.AnimationSet.NumAnimations);
  96.                 cmd.CommandText = query;
  97.         //var cmd = new SQLiteCommand(query, Connection);
  98.             cmd.ExecuteNonQuery();
  99.             foreach(Animation ani in a.AnimationSet.Animations)
  100.             {
  101.               var query2 =
  102.                         string.Format("INSERT INTO Animations (animindex, sno, name, tagid) VALUES({0},{1},'{2}',{3})",anims,ani.AniSNO,ani.name,ani.AniTagID);
  103.             //var cmd2 = new SQLiteCommand(query2, Connection);
  104.               cmd.CommandText = query2;
  105.            
  106.             cmd.ExecuteNonQuery();
  107.             anims++;
  108.             }
  109.              
  110.             }
  111.             trans.Commit();
  112.             Connection.Close();
  113.  
  114.         }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.         public static AnimSet GetAnimSetByID(int ActorID)
  122.         {
  123.             SQLiteConnection Connection = new SQLiteConnection("Data Source=animation.db");
  124.             Connection.Open();
  125.             SQLiteCommand cmd = new SQLiteCommand(Connection);
  126.             cmd.CommandText = "SELECT * from anim WHERE actorsno=" + ActorID;
  127.             var reader = cmd.ExecuteReader();
  128.             if (!reader.HasRows) return null;
  129.             reader.Read();
  130.             int start = reader.GetInt32(1); int end = reader.GetInt32(2);
  131.             reader.Close();
  132.             cmd.CommandText = "SELECT * from Animations WHERE animindex >" + start + " AND animindex <" + end;
  133.             reader = cmd.ExecuteReader();
  134.             AnimSet anim = new AnimSet();
  135.  
  136.             while (reader.Read())
  137.             {
  138.                 Animation ani = new Animation();
  139.                 ani.AniSNO = reader.GetInt32(1);
  140.                 ani.name = reader.GetString(2);
  141.                 ani.AniTagID = reader.GetInt32(3);
  142.                 anim.Animations.Add(ani);
  143.             }
  144.             Connection.Close();
  145.             return anim;
  146.         }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.         int ReadInt32(FileStream fs)
  155.         {
  156.             byte[] b = new byte[4];
  157.             int result = fs.Read(b, 0, 4);
  158.             return BitConverter.ToInt32(b, 0);
  159.         }
  160.         List<AnimSet> ParseAnimSet()
  161.         {
  162.             List<AnimSet> list = new List<AnimSet>();
  163.             var fullpath = path + "AnimSet\\";
  164.             foreach (string s in Directory.EnumerateFiles(fullpath))
  165.             {
  166.  
  167.                 AnimSet a = new AnimSet();
  168.                 a.Animations = new List<Animation>();
  169.                 FileStream fs = File.OpenRead(s);
  170.                 fs.Seek(16, SeekOrigin.Begin);
  171.                 int AnimSetSNO = ReadInt32(fs);
  172.                 a.AnimsetSNO = AnimSetSNO;
  173.                 fs.Position = 352;
  174.                 int NumAnimations = ReadInt32(fs);
  175.                 a.NumAnimations = NumAnimations;
  176.                 for (int i = 0; i <= NumAnimations; i++)
  177.                 {
  178.                     fs.Position += 4;
  179.                     int TagID = ReadInt32(fs);
  180.                     int AniSNO = ReadInt32(fs);
  181.                     Animation ani = new Animation();
  182.                     ani.AniSNO = AniSNO;
  183.                     ani.AniTagID = TagID;
  184.                     a.Animations.Add(ani);
  185.                 }
  186.                 list.Add(a);
  187.                     fs.Close();
  188.                
  189.             }
  190.             return list;
  191.         }
  192.         List<Actor> ParseActor()
  193.         {
  194.             List<Actor> mylist = new List<Actor>();
  195.             var fullpath = path + "actor\\";
  196.             foreach (string s in Directory.EnumerateFiles(fullpath))
  197.             {
  198.                 FileStream fs = File.OpenRead(s);
  199.                 fs.Seek(16, SeekOrigin.Begin);
  200.  
  201.                 int ActorSNO = ReadInt32(fs);
  202.                 fs.Position = 120;
  203.                 int AnimsetSNO = ReadInt32(fs);
  204.                 fs.Close();
  205.                 Actor a = new Actor();
  206.                 a.ActorSNO = ActorSNO;
  207.                 a.AnimSetSNO = AnimsetSNO;
  208.                 a.AnimationSet = new AnimSet();
  209.                 a.AnimationSet.AnimsetSNO = AnimsetSNO;
  210.  
  211.                 mylist.Add(a);
  212.                
  213.                 fs.Close();
  214.             }
  215.  
  216.             return mylist;
  217.        
  218.         }
  219.  
  220.         byte[] bytestillnull(FileStream fs)
  221.         {
  222.             List<byte> list = new List<byte>();
  223.             while (true)
  224.             {
  225.                 byte b = (byte)fs.ReadByte();
  226.                 if (b == 0) { if (fs.ReadByte() == 0) { return list.ToArray(); } else { fs.Position += -1; } }
  227.                 list.Add(b);
  228.             }
  229.             return null;
  230.  
  231.         }
  232.  
  233.         void ParseAni(List<AnimSet> list)
  234.         {
  235.            
  236.             var fullpath = path + "Anim\\";
  237.             foreach (string s in Directory.EnumerateFiles(fullpath))
  238.             {
  239.                 FileStream fs = File.OpenRead(s);
  240.                 fs.Seek(16, SeekOrigin.Begin);
  241.                 int aniSNO = ReadInt32(fs);
  242.                 string name = "";
  243.                 fs.Position = 76; // WRONG some ani's it starts earlier NEED TO FIX
  244.                 name = System.Text.ASCIIEncoding.ASCII.GetString(bytestillnull(fs));
  245.                 try { list.Single(a => a.Animations.Exists(az => az.AniSNO == aniSNO)).GetAniByID(aniSNO).name = name;
  246.                 Console.WriteLine(name);
  247.                 }
  248.                 catch { }
  249.             }
  250.  
  251.             var fa = File.CreateText(path + "all.txt");
  252.             foreach (var pv in list)
  253.             {
  254.                
  255.             }
  256.             fa.Close();
  257.         }
  258.     }
  259. }
Add Comment
Please, Sign In to add comment