Beispiel #1
0
        private void mnuInsertArrayItem_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
                PropertyModel     m     = (PropertyModel)((MenuItem)sender).DataContext;
                int arrayIndex          = displayProperties.IndexOf(m);

                int lastIndex = displayProperties.Where(p => p.Id == m.Id).Max(p => p.ArrayIndex);
                int maxIndex  = displayProperties.IndexOf(displayProperties.Last(p => p.Id == m.Id && p.ArrayIndex == lastIndex));

                PropertyModel newModel = new PropertyModel(m.Id, m.Value, true, lastIndex + 1);

                int indexOfProperty = displayProperties.IndexOf(m);
                displayProperties.Insert(arrayIndex, newModel);

                List <PropertyModel> arrayProperties = displayProperties.Where(p => p.Id == m.Id).ToList();
                for (int i = 0; i < (arrayProperties.Count - 1); i++)
                {
                    arrayProperties[i].ArrayIndex = i;
                }

                Value_PropertyChanged(this, new EventArgs());

                dataGrid1.Items.Refresh();
            }
        }
Beispiel #2
0
        private void btnLightsEditor_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                ViewLotEditor editor = new ViewLotEditor(index.Index, FlattenToPropertiesDictionary());
                if (editor.ShowDialog().GetValueOrDefault(false))
                {
                    //     displayProperties = editor.Properties;
                    //Value_PropertyChanged(this, new EventArgs());

                    //if the text has been changed, create a new modifiedIndex
                    DatabaseIndex originalIndex = DatabaseManager.Instance.Find(i => i.TypeId == index.Index.TypeId &&
                                                                                i.GroupContainer == index.Index.GroupContainer &&
                                                                                i.InstanceId == index.Index.InstanceId);
                    originalIndex.IsModified = true;
                    ModifiedPropertyFile newpropertyFile = new ModifiedPropertyFile();
                    newpropertyFile.PropertyFile               = new PropertyFile();
                    newpropertyFile.PropertyFile.Values        = editor.UnitFileEntry.Save();
                    newpropertyFile.PropertyFile.PropertyCount = (uint)newpropertyFile.PropertyFile.Values.Count;
                    originalIndex.ModifiedData = newpropertyFile;

                    ReloadDisplayProperties(newpropertyFile.PropertyFile.Values);
                }
            }
        }
Beispiel #3
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                if (index.Index.TypeId == 0x02393756 && index.Data[0] == 0) //cursor or ico file, there is a riff file which contains a list of icons, hence the check for the first byte
                {
                    index.Data[2] = 1;                                      //cursor files are ico files except the 3rd byte is 2, not 1. IconBitmapDecoder requires the 3rd byte to be 1.
                    MemoryStream byteStream = new MemoryStream(index.Data);
                    try
                    {
                        IconBitmapDecoder decoder = new IconBitmapDecoder(byteStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
                        imagePreview.Source = decoder.Frames[0].Thumbnail;
                        txtSize.Text        = decoder.Frames.Count.ToString();
                    }
                    catch
                    {
                    }
                }
                else
                {
                    LoadImage(index.Data);
                }
            }
        }
        private void textBoxJavaScript_TextChanged(object sender, TextChangedEventArgs e)
        {
            DatabaseIndexData index = this.DataContext as DatabaseIndexData;

            if (index != null && this.isDirty)
            {
                //if the text has been changed, create a new modifiedIndex
                Window     owner = Window.GetWindow(this);
                MainWindow main  = null;
                if (owner is MainWindow)
                {
                    main = owner as MainWindow;
                }
                else if (owner is ViewWindow)
                {
                    main = (owner as ViewWindow).Main;
                }
                if (main != null)
                {
                    DatabaseIndex originalIndex = DatabaseManager.Instance.Find(i => i.TypeId == index.Index.TypeId &&
                                                                                i.GroupContainer == index.Index.GroupContainer &&
                                                                                i.InstanceId == index.Index.InstanceId);
                    originalIndex.IsModified = true;
                    ModifiedTextFile textFile = new ModifiedTextFile();
                    textFile.Text = textBoxJavaScript.Text;
                    originalIndex.ModifiedData = textFile;
                }
            }
            this.isDirty = true;
        }
Beispiel #5
0
 private void Grid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
     if (this.DataContext != null)
     {
         if (this.DataContext.GetType() == typeof(DatabaseIndexData))
         {
             DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
             ByteArrayToIndexedHexStringConverter converter = new ByteArrayToIndexedHexStringConverter();
             textBoxHex.Text     = (string)converter.Convert(index.Data, typeof(string), "8", CultureInfo.CurrentCulture);
             textBoxRawData.Text = System.Text.ASCIIEncoding.ASCII.GetString(index.Data);
         }
         else if (this.DataContext.GetType() == typeof(byte[]))
         {
             byte[] data = (byte[])this.DataContext;
             ByteArrayToIndexedHexStringConverter converter = new ByteArrayToIndexedHexStringConverter();
             textBoxHex.Text     = (string)converter.Convert(data, typeof(string), "8", CultureInfo.CurrentCulture);
             textBoxRawData.Text = System.Text.ASCIIEncoding.ASCII.GetString(data);
         }
         else if (this.DataContext.GetType() == typeof(RW4Section))
         {
             // RW4Section section = (RW4Section)this.DataContext;
             // ByteArrayToIndexedHexStringConverter converter = new ByteArrayToIndexedHexStringConverter();
             // textBoxHex.Text = (string)converter.Convert(section.obj.Data, typeof(string), "8", CultureInfo.CurrentCulture);
             // textBoxRawData.Text = System.Text.ASCIIEncoding.ASCII.GetString(section.obj);
         }
     }
 }
Beispiel #6
0
        SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null && item is DatabaseIndexData)
            {
                DatabaseIndexData index = item as DatabaseIndexData;

                string viewer = TGIRegistry.Instance.FileTypes.GetViewer(index.Index.TypeId);
                if (index.Index.InstanceType == (uint)PropertyFileTypeIds.DecalAtlas || index.Index.InstanceType == (uint)PropertyFileTypeIds.DecalAtlas2 || index.Index.InstanceType == (uint)PropertyFileTypeIds.DecalAtlas3)
                {
                    if (element.TryFindResource("viewDecalDictionary") != null)
                    {
                        return(element.FindResource("viewDecalDictionary") as DataTemplate);
                    }
                }
                if (index.Index.InstanceType == (uint)PropertyFileTypeIds.Path)
                {
                    if (element.TryFindResource("viewPath") != null)
                    {
                        return(element.FindResource("viewPath") as DataTemplate);
                    }
                }
                if (viewer != "")
                {
                    return(element.FindResource(viewer) as DataTemplate);
                }
                else
                {
                    return(element.FindResource("viewData") as DataTemplate);
                }
            }
            return(null);
        }
Beispiel #7
0
        private void LoadSection(RW4Section section)
        {
            if (section != null)
            {
                if (section.GetType() == typeof(RW4Section))
                {
                    try
                    {
                        viewContainer.Content = new RW4ModelSectionView()
                        {
                            Section = section, Model = _rw4model
                        };
                    }
                    catch { }
                    try
                    {
                        DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                        byte[] buffer = new byte[section.Size];
                        using (Stream stream = new MemoryStream(index.Data))
                        {
                            stream.Seek((int)section.Pos, SeekOrigin.Begin);
                            stream.Read(buffer, 0, (int)section.Size);
                        }
                        viewHex.DataContext = buffer;
                    }
                    catch
                    {
                    }
                }
            }
        }
Beispiel #8
0
 private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
     if (this.DataContext != null && this.DataContext.GetType() == typeof(DatabaseIndexData))
     {
         DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
         LoadImage(index.Data);
     }
 }
Beispiel #9
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null)
            {
                if (this.DataContext.GetType() == typeof(DatabaseIndexData))
                {
                    DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                    int          i      = 0;
                    StringReader reader = new StringReader(textBoxHex.Text);
                    while (reader.Peek() != -1)
                    {
                        string   line   = reader.ReadLine();
                        string[] values = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int j = 0; j < 8; j++)
                        {
                            (index.Data as byte[])[i + j] = byte.Parse(values[j + 1].ToString(), NumberStyles.AllowHexSpecifier);
                        }
                        i += 8;
                    }


                    index.Index.ModifiedData = new ModifiedGenericFile()
                    {
                        FileData = index.Data
                    };
                    index.Index.IsModified = true;
                    // DataChanged();

                    //
                    // ByteArrayToIndexedHexStringConverter converter = new ByteArrayToIndexedHexStringConverter();
                    //  textBoxHex.Text = (string)converter.Convert(index.Data, typeof(string), "8", CultureInfo.CurrentCulture);
                    //  textBoxRawData.Text = System.Text.ASCIIEncoding.ASCII.GetString(index.Data);
                }
                if (this.DataContext.GetType() == typeof(byte[]))
                {
                    byte[] data = this.DataContext as byte[];

                    int          i      = 0;
                    StringReader reader = new StringReader(textBoxHex.Text);
                    while (reader.Peek() != -1)
                    {
                        string   line   = reader.ReadLine();
                        string[] values = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int j = 0; j < 8; j++)
                        {
                            if (j + 1 < values.Length)
                            {
                                (this.DataContext as byte[])[i + j] = byte.Parse(values[j + 1].ToString(), NumberStyles.AllowHexSpecifier);
                            }
                        }
                        i += 8;
                    }

                    DataChanged();
                }
            }
        }
Beispiel #10
0
        private void LoadImage()
        {
            try
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
                if (index != null)
                {
                    using (MemoryStream byteStream = new MemoryStream(index.Data))
                    {
                        RasterChannel channel = RasterChannel.Preview;
                        if (rAll.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.All;
                        }
                        if (rCombined.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.Preview;
                        }
                        if (rAlpha.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.A;
                        }
                        if (rRed.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.R;
                        }
                        if (rGreen.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.G;
                        }
                        if (rBlue.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.B;
                        }
                        if (rFacadeColor.IsChecked.GetValueOrDefault(false))
                        {
                            channel = RasterChannel.FacadeColor;
                        }

                        RasterImage = RasterImage.CreateFromStream(byteStream, channel);

                        imagePreview.Source  = RasterImage.MipMaps[0];
                        imagePreview.Width   = RasterImage.MipMaps[0].PixelWidth;
                        imagePreview.Height  = RasterImage.MipMaps[0].PixelHeight;
                        imagePreview.Stretch = Stretch.None;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #11
0
        private void btnViewChildren_Click(object sender, RoutedEventArgs e)
        {
            MainWindow main = getMainWindow();

            if (main != null)
            {
                ViewSearchProperties res   = new ViewSearchProperties(main);
                DatabaseIndexData    index = (DatabaseIndexData)this.DataContext;
                res.txtSearchValue.Text = index.Index.InstanceId.ToHex();
                res.txtPropertyID.Text  = "0x00b2cccb";
                res.startSearch();
                res.Show();
            }
        }
Beispiel #12
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                MemoryStream byteStream = new MemoryStream(index.Data);
                try
                {
                    EffectsDirectory effDir = EffectsDirectory.CreateFromStream(byteStream);
                }
                catch
                {
                }
            }
        }
Beispiel #13
0
        private void textBoxRawData_TextChanged(object sender, TextChangedEventArgs e)
        {
            DatabaseIndexData index = this.DataContext as DatabaseIndexData;

            if (index != null && this.isDirty)
            {
                //if the text has been changed, create a new modifiedIndex
                DatabaseIndex originalIndex = DatabaseManager.Instance.Find(i => i.TypeId == index.Index.TypeId &&
                                                                            i.GroupContainer == index.Index.GroupContainer &&
                                                                            i.InstanceId == index.Index.InstanceId);
                originalIndex.IsModified = true;
                ModifiedTextFile textFile = new ModifiedTextFile();
                textFile.Text = textBoxRawData.Text;
                originalIndex.ModifiedData = textFile;
            }
            this.isDirty = true;
        }
Beispiel #14
0
 void Value_PropertyChanged(object sender, EventArgs e)
 {
     if (this.DataContext.GetType() == typeof(DatabaseIndexData))
     {
         DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
         //if the text has been changed, create a new modifiedIndex
         DatabaseIndex originalIndex = DatabaseManager.Instance.Find(i => i.TypeId == index.Index.TypeId &&
                                                                     i.GroupContainer == index.Index.GroupContainer &&
                                                                     i.InstanceId == index.Index.InstanceId);
         originalIndex.IsModified = true;
         ModifiedPropertyFile propertyFile = new ModifiedPropertyFile();
         propertyFile.PropertyFile               = new PropertyFile();
         propertyFile.PropertyFile.Values        = FlattenToPropertiesDictionary();
         propertyFile.PropertyFile.PropertyCount = (uint)propertyFile.PropertyFile.Values.Count;
         originalIndex.ModifiedData              = propertyFile;
     }
 }
Beispiel #15
0
        private void btnDebug_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
                //if the text has been changed, create a new modifiedIndex

                DatabaseIndex originalIndex = DatabaseManager.Instance.Find(i => i.TypeId == index.Index.TypeId &&
                                                                            i.GroupContainer == index.Index.GroupContainer &&
                                                                            i.InstanceId == index.Index.InstanceId);
                //  originalIndex.IsModified = true;
                ModifiedPropertyFile propertyFile = new ModifiedPropertyFile();
                propertyFile.PropertyFile        = new PropertyFile();
                propertyFile.PropertyFile.Values = new Dictionary <uint, Property>();
                foreach (PropertyModel prop in displayProperties)
                {
                    if (prop.IsArray)
                    {
                        if (prop.ArrayIndex == 0)
                        {
                            ArrayProperty arrayProp = new ArrayProperty();
                            arrayProp.PropertyType = prop.Value.GetType();
                            arrayProp.Values.Add(prop.Value);

                            propertyFile.PropertyFile.Values.Add(prop.Id, arrayProp);
                        }
                        else
                        {
                            ArrayProperty arrayProp = (ArrayProperty)propertyFile.PropertyFile.Values[prop.Id];
                            arrayProp.Values.Add(prop.Value);
                        }
                    }
                    else
                    {
                        propertyFile.PropertyFile.Values.Add(prop.Id, prop.Value);
                    }
                }
                propertyFile.PropertyFile.PropertyCount = (uint)propertyFile.PropertyFile.Values.Count;
                //
                // originalIndex.ModifiedData = propertyFile;

                ViewHexDiff diff = new ViewHexDiff(index.Data, propertyFile.GetData());
                diff.ShowDialog();
            }
        }
Beispiel #16
0
 private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
     if (this.DataContext != null)
     {
         this.isDirty = false;
         if (this.DataContext is DatabaseIndexData)
         {
             DatabaseIndexData index = this.DataContext as DatabaseIndexData;
             textBoxRawData.Text = ASCIIEncoding.UTF8.GetString(index.Data);
         }
         else if (this.DataContext is byte[])
         {
             byte[] data = this.DataContext as byte[];
             textBoxRawData.Text = ASCIIEncoding.UTF8.GetString(data);
         }
         textBoxRawData.ScrollToHome();
         txtLineNumber.Text = "0";
         this.textFind.Reset(); //New content is loaded so reset search to start position
     }
 }
Beispiel #17
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null)
            {
                try
                {
                    DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                    _rw4model = new RW4Model();
                    using (Stream stream = new MemoryStream(index.Data))
                    {
                        _rw4model.Read(stream);
                    }
                    //textBlockRW4Type.Text = model.FileType.ToString();

                    // gridHeaderDetails.DataContext = _rw4model.Header;
                    txtHeaderSectionBegin.Text = _rw4model.Header.SectionIndexBegin.ToString();
                    txtHeaderPadder.Text       = _rw4model.Header.SectionIndexPadding.ToString();
                    txtHeaderEnd.Text          = _rw4model.Header.SectionIndexEnd.ToString();
                    txtHeaderEndPos.Text       = _rw4model.Header.HeaderEnd.ToString();

                    dataGrid1.ItemsSource = _rw4model.Sections;

                    _rw4model.Sections.ForEach(t => t.SectionChanged += new EventHandler(section_SectionChanged));

                    RW4Section sec = _rw4model.Sections.FirstOrDefault(s => s.TypeCode == SectionTypeCodes.Mesh);
                    if (sec != null)
                    {
                        dataGrid1.SelectedItem = sec;
                    }
                    else
                    {
                        RW4Section sec2 = _rw4model.Sections.FirstOrDefault(s => s.TypeCode == SectionTypeCodes.Texture);
                        dataGrid1.SelectedItem = sec2;
                    }
                }
                catch
                {
                }
            }
        }
 private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
     if (this.DataContext != null)
     {
         this.isDirty = false;
         if (this.DataContext is DatabaseIndexData)
         {
             DatabaseIndexData index = this.DataContext as DatabaseIndexData;
             StringToJS        conv  = new StringToJS();
             textBoxJavaScript.Text = (string)conv.Convert(ASCIIEncoding.UTF8.GetString(index.Data), typeof(string), null, CultureInfo.CurrentCulture);
         }
         else if (this.DataContext is byte[])
         {
             byte[]     data = this.DataContext as byte[];
             StringToJS conv = new StringToJS();
             textBoxJavaScript.Text = (string)conv.Convert(ASCIIEncoding.UTF8.GetString(data), typeof(string), null, CultureInfo.CurrentCulture);
         }
         textBoxJavaScript.ScrollToHome();
         txtLineNumber.Text = "0";
         this.textFind.Reset(); //New content is loaded so reset search to start position
     }
 }
Beispiel #19
0
        private void viewHex_DataChangedHandler(object sender, EventArgs e)
        {
            if (dataGrid1.SelectedItem != null)
            {
                if (dataGrid1.SelectedItem.GetType() == typeof(RW4Section))
                {
                    RW4Section section = (RW4Section)dataGrid1.SelectedItem;

                    DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                    byte[] buffer = viewHex.DataContext as byte[];
                    //   using (Stream stream = new MemoryStream(index.Data))
                    //  {
                    //      stream.Seek((int)section.Pos, SeekOrigin.Begin);
                    //      stream.Write(buffer, 0, (int)section.Size);
                    //  }
                    (section.obj as RW4Blob).blob = buffer;

                    SaveRW4Model();
                }
            }
        }
Beispiel #20
0
        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                byte[] data = File.ReadAllBytes(dlg.FileName);

                if (this.DataContext.GetType() == typeof(DatabaseIndexData))
                {
                    DatabaseIndexData index = (DatabaseIndexData)this.DataContext;
                    //if the text has been changed, create a new modifiedIndex

                    Window     owner = Window.GetWindow(this);
                    MainWindow main  = null;
                    if (owner is MainWindow)
                    {
                        main = owner as MainWindow;
                    }
                    else if (owner is ViewWindow)
                    {
                        main = ((ViewWindow)owner).Main;
                    }
                    if (main != null)
                    {
                        DatabaseIndex originalIndex = DatabaseManager.Instance.Find(i => i.TypeId == index.Index.TypeId &&
                                                                                    i.GroupContainer == index.Index.GroupContainer &&
                                                                                    i.InstanceId == index.Index.InstanceId);
                        originalIndex.IsModified = true;
                        ModifiedTgaFile propertyFile = new ModifiedTgaFile();
                        propertyFile.ImageFileData = data;
                        originalIndex.ModifiedData = propertyFile;

                        LoadImage(data);
                    }
                }
            }
        }
Beispiel #21
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData           index = (DatabaseIndexData)this.DataContext;
                Dictionary <uint, Property> propertyContainer;

                try
                {
                    using (MemoryStream byteStream = new MemoryStream(index.Data))
                    {
                        propertyFile = new PropertyFile();
                        propertyFile.Read(byteStream);
                        propertyContainer = propertyFile.Values;

                        pnlImages.Children.RemoveRange(0, pnlImages.Children.Count);
                        displayProperties = new List <PropertyModel>();

                        ReloadDisplayProperties(propertyContainer);
                    }
                }
                catch (Exception ex)
                {
                    tbxError.Visibility  = System.Windows.Visibility.Visible;
                    dataGrid1.Visibility = System.Windows.Visibility.Collapsed;
                    string    exceptionMessage = ex.Message + Environment.NewLine;
                    Exception subEx            = ex.InnerException;
                    while (subEx != null)
                    {
                        exceptionMessage += subEx.Message + Environment.NewLine;
                        subEx             = ex.InnerException;
                    }

                    tbxError.Text = exceptionMessage + Environment.NewLine + ex.StackTrace;
                }
            }
        }
Beispiel #22
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.DefaultExt = "bmp";
            dlg.Filter     = "BMP|*.bmp";
            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                BitmapImage bitmap = new BitmapImage(new Uri(dlg.FileName));

                SimCityPak.ExtensionMethods.PixelColor[,] pixels = bitmap.GetPixels();
                int pixelSize = 1;
                if (DataType == 7)
                {
                    pixelSize = 2;
                }
                if (DataType == 2)
                {
                    pixelSize = 4;
                }


                int imageByteSize = (5 * 4) + (bitmap.PixelHeight * bitmap.PixelWidth * pixelSize);

                byte[] rasterData = new byte[imageByteSize];


                using (MemoryStream byteStream = new MemoryStream(rasterData))
                {
                    byteStream.WriteU32BE(Unknown1);
                    byteStream.WriteS32BE(bitmap.PixelWidth);
                    byteStream.WriteS32BE(bitmap.PixelHeight);
                    byteStream.WriteU32BE(DataType);
                    byteStream.WriteU32BE((uint)(bitmap.PixelHeight * bitmap.PixelWidth * pixelSize));

                    for (int i = 0; i < bitmap.PixelWidth; i++)
                    {
                        for (int j = 0; j < bitmap.PixelHeight; j++)
                        {
                            if (DataType == 7)
                            {
                                byteStream.WriteU16((ushort)(pixels[j, i].Red * 8));
                            }
                            if (DataType == 2)
                            {
                                byteStream.WriteU32((uint)(pixels[j, i].Red * 16));
                            }
                            if (DataType == 1)
                            {
                                byteStream.WriteU8(pixels[j, i].Red);
                            }
                        }
                    }
                }

                index.Index.ModifiedData = new ModifiedRawImage()
                {
                    ImageFileData = rasterData
                };
                index.Index.IsModified = true;
            }
        }
Beispiel #23
0
        private void mnuImportDDS_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.DefaultExt = "dds";
            dlg.Filter     = "DDS|*.dds";
            //needs to be an 8.8.8.8 ARGB texture
            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                using (Stream strm = File.OpenRead(dlg.FileName))
                {
                    strm.expect(0x20534444, "DDS000");  // 'DDS '
                    int headerSize = strm.ReadS32();
                    if (headerSize < 0x7C)
                    {
                        throw new ModelFormatException(strm, "DDS001", headerSize);
                    }
                    var flags             = strm.ReadU32();
                    var height            = strm.ReadS32();
                    var width             = strm.ReadS32();
                    var pitchOrLinearSize = strm.ReadU32();
                    strm.expect(0, "DDS002");  // < depth
                    var mipmaps = strm.ReadS32();

                    strm.Seek(strm.Position + 11 * 4, SeekOrigin.Begin);
                    var pfsize = strm.ReadS32();
                    if (pfsize < 32)
                    {
                        throw new ModelFormatException(strm, "DDS011", pfsize);
                    }
                    var pf_flags = strm.ReadU32();
                    var fourcc   = strm.ReadU32();

                    strm.Seek(headerSize + 4, SeekOrigin.Begin);
                    var sizes = (from i in Enumerable.Range(0, mipmaps)
                                 select(Math.Max(width >> i, 1) * Math.Max(height >> i, 1) * 4)   // DXT5: 16 bytes per 4x4=16 pixels
                                 ).ToArray();
                    var all_mipmaps = new byte[sizes.Sum()];
                    for (int offset = 0, i = 0; i < mipmaps; i++)
                    {
                        if (strm.Read(all_mipmaps, offset, sizes[i]) != sizes[i])
                        {
                            throw new ModelFormatException(strm, "Unexpected EOF reading .DDS file", null);
                        }
                        offset += sizes[i];
                    }

                    byte[] rasterData = new byte[all_mipmaps.Length + (4 * 6) + (sizes.Length * 4)];


                    using (MemoryStream byteStream = new MemoryStream(rasterData))
                    {
                        byteStream.WriteU32BE(RasterImage.RasterType);
                        byteStream.WriteU32BE((uint)width);
                        byteStream.WriteU32BE((uint)height);
                        byteStream.WriteU32BE((uint)sizes.Length);
                        byteStream.WriteU32BE((uint)8);
                        byteStream.WriteU32BE((uint)RasterImage.RasterPixelFormat);


                        int position = 0;
                        foreach (int mipMapSize in sizes.OrderByDescending(m => m))
                        {
                            //int blockSize = height * width * 4;
                            byteStream.WriteU32BE((uint)mipMapSize);



                            for (int j = 0; j < mipMapSize; j++)
                            {
                                byteStream.WriteU8(all_mipmaps[position]);
                                position++;
                            }
                        }
                    }

                    index.Index.ModifiedData = new ModifiedRasterFile()
                    {
                        ImageFileData = rasterData
                    };
                    index.Index.IsModified = true;


                    LoadImage();
                }
            }
        }
Beispiel #24
0
        private void mnuCopy_Click(object sender, RoutedEventArgs e)
        {
            //check if current index is a valid menu item
            if (this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData menuItemIndex = (DatabaseIndexData)this.DataContext;
                menuItemIndex.Index.IsModified = true;
                if (menuItemIndex.Index.InstanceType == (uint)PropertyFileTypeIds.Menu2)
                {
                    SaveFileDialog dialog = new SaveFileDialog();
                    if (dialog.ShowDialog().GetValueOrDefault(false))
                    {
                        List <DatabaseIndex> indices = new List <DatabaseIndex>();

                        menuItemIndex.Index.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                        indices.Add(menuItemIndex.Index);

                        PropertyFile menuFile = new PropertyFile(menuItemIndex.Index);

                        #region Extract Icon

                        if (menuFile.Values.ContainsKey(PropertyConstants.menuIcon))
                        {
                            KeyProperty   iconProperty = (KeyProperty)menuFile.Values[PropertyConstants.menuIcon];
                            DatabaseIndex iconIndex    = DatabaseIndex.FindIndex(iconProperty.TypeId, null, null, iconProperty.InstanceId, true, false);

                            iconIndex.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                            indices.Add(iconIndex);

                            iconProperty.InstanceId          = iconIndex.ModifiedInstanceId.Value;
                            menuItemIndex.Index.ModifiedData = new ModifiedPropertyFile()
                            {
                                PropertyFile = menuFile
                            };

                            menuFile.Values[PropertyConstants.menuIcon] = iconProperty;
                        }

                        #endregion

                        #region Extract Marquee Image

                        if (menuFile.Values.ContainsKey(PropertyConstants.menuMarqueeImage))
                        {
                            KeyProperty   marqueeProperty = (KeyProperty)menuFile.Values[PropertyConstants.menuMarqueeImage];
                            DatabaseIndex marqueeIndex    = DatabaseIndex.FindIndex(marqueeProperty.TypeId, null, null, marqueeProperty.InstanceId, true, false);

                            marqueeIndex.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                            indices.Add(marqueeIndex);

                            marqueeProperty.InstanceId       = marqueeIndex.ModifiedInstanceId.Value;
                            menuItemIndex.Index.ModifiedData = new ModifiedPropertyFile()
                            {
                                PropertyFile = menuFile
                            };

                            menuFile.Values[PropertyConstants.menuMarqueeImage] = marqueeProperty;
                        }

                        #endregion

                        #region Extract Ecogame Unit

                        if (menuFile.Values.ContainsKey(PropertyConstants.menuUnit))
                        {
                            KeyProperty   unitProperty = (KeyProperty)menuFile.Values[PropertyConstants.menuUnit];
                            DatabaseIndex unitIndex    = DatabaseIndex.FindIndex((uint)TypeIds.PropertyFile, null, 0xe0, unitProperty.InstanceId, false, true);

                            PropertyFile unitFile = new PropertyFile(unitIndex);
                            unitFile.FlattenParentInheritance();

                            #region Extract Unit Display

                            if (unitFile.Values.ContainsKey(PropertyConstants.ecoGameUnitDisplayModel))
                            {
                                KeyProperty   unitDisplayProperty = (KeyProperty)unitFile.Values[PropertyConstants.ecoGameUnitDisplayModel];
                                DatabaseIndex unitDisplayIndex    = DatabaseIndex.FindIndex((uint)TypeIds.PropertyFile, null, 0xe1, unitDisplayProperty.InstanceId, false, true);

                                unitDisplayIndex.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                                unitDisplayProperty.InstanceId      = unitDisplayIndex.ModifiedInstanceId.Value;

                                unitFile.Values[PropertyConstants.ecoGameUnitDisplayModel] = unitDisplayProperty;

                                PropertyFile unitDisplayFile = new PropertyFile(unitDisplayIndex);

                                #region Extract LODs

                                if (unitDisplayFile.Values.ContainsKey(PropertyConstants.UnitLOD1))
                                {
                                    KeyProperty   LOD1Property = (KeyProperty)unitDisplayFile.Values[PropertyConstants.UnitLOD1];
                                    DatabaseIndex LOD1Index    = DatabaseIndex.FindIndex(LOD1Property.TypeId, null, null, LOD1Property.InstanceId, true, false);
                                    LOD1Index.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                                    indices.Add(LOD1Index);
                                    LOD1Property.InstanceId = LOD1Index.ModifiedInstanceId.Value;
                                    unitDisplayFile.Values[PropertyConstants.UnitLOD1] = LOD1Property;
                                }

                                if (unitDisplayFile.Values.ContainsKey(PropertyConstants.UnitLOD2))
                                {
                                    KeyProperty   LOD2Property = (KeyProperty)unitDisplayFile.Values[PropertyConstants.UnitLOD2];
                                    DatabaseIndex LOD2Index    = DatabaseIndex.FindIndex(LOD2Property.TypeId, null, null, LOD2Property.InstanceId, true, false);
                                    LOD2Index.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                                    indices.Add(LOD2Index);
                                    LOD2Property.InstanceId = LOD2Index.ModifiedInstanceId.Value;
                                    unitDisplayFile.Values[PropertyConstants.UnitLOD2] = LOD2Property;
                                }

                                if (unitDisplayFile.Values.ContainsKey(PropertyConstants.UnitLOD3))
                                {
                                    KeyProperty   LOD3Property = (KeyProperty)unitDisplayFile.Values[PropertyConstants.UnitLOD3];
                                    DatabaseIndex LOD3Index    = DatabaseIndex.FindIndex(LOD3Property.TypeId, null, null, LOD3Property.InstanceId, true, false);
                                    LOD3Index.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                                    indices.Add(LOD3Index);
                                    LOD3Property.InstanceId = LOD3Index.ModifiedInstanceId.Value;
                                    unitDisplayFile.Values[PropertyConstants.UnitLOD3] = LOD3Property;
                                }

                                if (unitDisplayFile.Values.ContainsKey(PropertyConstants.UnitLOD4))
                                {
                                    KeyProperty   LOD4Property = (KeyProperty)unitDisplayFile.Values[PropertyConstants.UnitLOD4];
                                    DatabaseIndex LOD4Index    = DatabaseIndex.FindIndex(LOD4Property.TypeId, null, null, LOD4Property.InstanceId, true, false);
                                    LOD4Index.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                                    indices.Add(LOD4Index);
                                    LOD4Property.InstanceId = LOD4Index.ModifiedInstanceId.Value;
                                    unitDisplayFile.Values[PropertyConstants.UnitLOD4] = LOD4Property;
                                }


                                #endregion

                                unitDisplayIndex.ModifiedData = new ModifiedPropertyFile()
                                {
                                    PropertyFile = unitDisplayFile
                                };
                                unitDisplayIndex.IsModified = true;

                                indices.Add(unitDisplayIndex);
                            }

                            #endregion

                            unitIndex.ModifiedInstanceId = TGIRandomGenerator.GetNext();
                            unitIndex.ModifiedData       = new ModifiedPropertyFile()
                            {
                                PropertyFile = unitFile
                            };
                            unitIndex.IsModified = true;

                            indices.Add(unitIndex);

                            unitProperty.InstanceId          = unitIndex.ModifiedInstanceId.Value;
                            menuItemIndex.Index.ModifiedData = new ModifiedPropertyFile()
                            {
                                PropertyFile = menuFile
                            };

                            menuFile.Values[PropertyConstants.menuUnit] = unitProperty;
                        }

                        #endregion
                        DatabasePackedFile.SaveAs(dialog.FileName, indices);
                    }
                }
                else
                {
                    MessageBox.Show("This only works for menu items...");
                }
            }
        }
Beispiel #25
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(DatabaseIndexData))
            {
                DatabaseIndexData index      = (DatabaseIndexData)this.DataContext;
                MemoryStream      byteStream = new MemoryStream(index.Data);

                Unknown1  = byteStream.ReadU32BE();
                Width     = byteStream.ReadS32BE();
                Height    = byteStream.ReadS32BE();
                DataType  = byteStream.ReadU32BE();
                ImageSize = byteStream.ReadU32BE();

                BitmapSource bs = null;
                float[]      data = new float[Width * Height];
                float        min = float.MaxValue, max = 0f;
                int          bits = 0;
                if (DataType == 7)
                {
                    bits = 16;
                    for (int i = 0; i < Width * Height; i++)
                    {
                        data[i] = ((float)byteStream.ReadU16()) / 65536f;
                        min     = Math.Min(data[i], min);
                        max     = Math.Max(data[i], max);
                    }
                }
                else if (DataType == 2)
                {
                    bits = 32;
                    for (int i = 0; i < Width * Height; i++)
                    {
                        data[i] = (float)(((double)byteStream.ReadU32()) / (double)UInt32.MaxValue);
                        min     = Math.Min(data[i], min);
                        max     = Math.Max(data[i], max);
                    }
                }
                else if (DataType == 1)
                {
                    bits = 8;
                    for (int i = 0; i < Width * Height; i++)
                    {
                        data[i] = ((float)byteStream.ReadU8()) / 255f;
                        min     = Math.Min(data[i], min);
                        max     = Math.Max(data[i], max);
                    }
                }

                if (checkBox1.IsChecked == true)
                {
                    //scale the values for better contrast
                    float mult = 1f / (max - min);
                    for (int i = 0; i < Width * Height; i++)
                    {
                        data[i] = (data[i] - min) * mult;
                    }
                }

                bs = BitmapSource.Create(Width, Height, 96, 96, PixelFormats.Gray32Float, null, data, Width * 4);

                try
                {
                    imagePreview.Source = bs;

                    txtHeight.Text = Height.ToString();
                    txtWidth.Text  = Width.ToString();
                    txtSize.Text   = ImageSize.ToString();

                    txtPixelFormat.Text = bits + "-bit greyscale";
                }
                catch
                {
                }
            }
        }
Beispiel #26
0
        private void SaveRW4Model()
        {
            //Save the new thing to a stream!

            if (this.DataContext != null)
            {
                //  try
                //  {

                ModifiedRW4File   modifiedData = new ModifiedRW4File();
                DatabaseIndexData index        = (DatabaseIndexData)this.DataContext;

                using (Stream stream = new MemoryStream(index.Data))
                {
                    //first read the current RW4model

                    List <RW4Section> sections = dataGrid1.ItemsSource as List <RW4Section>;
                    _rw4model.Sections = sections;

                    foreach (RW4Section section in _rw4model.Sections)
                    {
                        if (section.TypeCode == SectionTypeCodes.Texture)
                        {
                            SporeMaster.RenderWare4.Texture tex = section.obj as SporeMaster.RenderWare4.Texture;
                            _rw4model.Sections[(int)tex.texData.section.Number].obj = tex.texData;
                        }
                    }

                    RW4Section meshSection = _rw4model.Sections.Find(s => s.TypeCode == SectionTypeCodes.Mesh);
                    if (meshSection != null)
                    {
                        SporeMaster.RenderWare4.RW4Mesh mesh = meshSection.obj as SporeMaster.RenderWare4.RW4Mesh;

                        //update the bounding box

                        /*RW4Section bboxSection = _rw4model.Sections.Find(s => s.TypeCode == SectionTypeCodes.BBox);
                         * if (bboxSection != null)
                         * {
                         *  RW4BBox boundingBox = bboxSection.obj as RW4BBox;
                         *  if (meshSection != null)
                         *  {
                         *      boundingBox.minx = mesh.vertices.vertices.Min(v => v.X);
                         *      boundingBox.miny = mesh.vertices.vertices.Min(v => v.Y);
                         *      boundingBox.minz = mesh.vertices.vertices.Min(v => v.Z);
                         *
                         *      boundingBox.maxx = mesh.vertices.vertices.Max(v => v.X);
                         *      boundingBox.maxy = mesh.vertices.vertices.Max(v => v.Y);
                         *      boundingBox.maxz = mesh.vertices.vertices.Max(v => v.Z);
                         *
                         *      bboxSection.obj = boundingBox;
                         *  }
                         * }*/

                        _rw4model.Sections[(int)mesh.vertices.section.Number].obj  = mesh.vertices;
                        _rw4model.Sections[(int)mesh.triangles.section.Number].obj = mesh.triangles;

                        _rw4model.Sections[(int)mesh.vertices.vertices.section.Number].obj   = mesh.vertices.vertices.section.obj;
                        _rw4model.Sections[(int)mesh.triangles.triangles.section.Number].obj = mesh.triangles.triangles.section.obj;
                    }



                    //save back the model

                    using (MemoryStream writer = new MemoryStream())
                    {
                        _rw4model.Write(writer);

                        modifiedData.RW4FileData = writer.ToArray();
                        index.Index.ModifiedData = modifiedData;
                        index.Index.IsModified   = true;
                        index.Index.Compressed   = false;
                    }
                }

                //ViewHexDiff hex = new ViewHexDiff(index.Data, modifiedData.RW4FileData);
                //hex.ShowDialog();
            }
        }