Skip to content

Commit 50de78d

Browse files
committed
Functionality to export .dff and .txd files
1 parent 2ca443d commit 50de78d

File tree

5 files changed

+135
-10
lines changed

5 files changed

+135
-10
lines changed

RenderWarePreviewer/Helpers/AssetHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ public Dff GetDff(string name)
6666
return dff;
6767
}
6868

69+
public byte[] GetRawDff(string name)
70+
{
71+
if (this.img == null || this.intImg == null)
72+
throw new FileNotFoundException("No img file found");
73+
74+
var key = SanitizeName(name) + ".dff";
75+
if (!this.img.Img.DataEntries.ContainsKey(key) && !this.intImg.Img.DataEntries.ContainsKey(key))
76+
throw new FileNotFoundException($"No {name}.dff file found in gta_int and img files");
77+
78+
var data = this.img.Img.DataEntries.ContainsKey(key) ? this.img.Img.DataEntries[key] : this.intImg.Img.DataEntries[key];
79+
80+
return data.Data;
81+
}
82+
6983
public Txd GetTxd(string name)
7084
{
7185
if (this.img == null || this.intImg == null)
@@ -84,6 +98,20 @@ public Txd GetTxd(string name)
8498
return txd;
8599
}
86100

101+
public byte[] GetRawTxd(string name)
102+
{
103+
if (this.img == null || this.intImg == null)
104+
throw new FileNotFoundException("No img file found");
105+
106+
var key = SanitizeName(name) + ".txd";
107+
if (!this.img.Img.DataEntries.ContainsKey(key) && !this.intImg.Img.DataEntries.ContainsKey(key))
108+
throw new FileNotFoundException($"No {name}.txd file found in gta_int and img files");
109+
110+
var data = this.img.Img.DataEntries.ContainsKey(key) ? this.img.Img.DataEntries[key] : this.intImg.Img.DataEntries[key];
111+
112+
return data.Data;
113+
}
114+
87115
public Dictionary<string, Image<Rgba32>> GetImages(Txd txd)
88116
{
89117
var images = new Dictionary<string, Image<Rgba32>>();

RenderWarePreviewer/Helpers/MeshHelper.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,43 @@ private static Point SanitzeUv(Uv uv)
148148
{
149149
//return new Point(uv.X, uv.Y);
150150
//return new Point((uv.X + 100f) % 1, (uv.Y + 100) % 1f);
151-
return new Point(
152-
uv.X < 0 ? uv.X + MathF.Round(MathF.Abs(uv.X) + 0.5f) :
153-
uv.X > 1 ? uv.X % 1f :
154-
uv.X,
155-
uv.Y < 0 ? uv.Y + MathF.Round(MathF.Abs(uv.Y) + 0.5f) :
156-
uv.Y > 1 ? uv.Y % 1f :
157-
uv.Y
158-
);
151+
//return new Point(
152+
// uv.X < 0 ? uv.X + MathF.Round(MathF.Abs(uv.X) + 0.5f) :
153+
// uv.X > 1 ? uv.X % 1f :
154+
// uv.X,
155+
// uv.Y < 0 ? uv.Y + MathF.Round(MathF.Abs(uv.Y) + 0.5f) :
156+
// uv.Y > 1 ? uv.Y % 1f :
157+
// uv.Y
158+
//);
159+
160+
//return new Point(
161+
// uv.X < -1 ? -(MathF.Abs(uv.X) % 1f) + MathF.Round(MathF.Abs(-(MathF.Abs(uv.X) % 1f)) + 0.5f) :
162+
// uv.X < 0 ? uv.X + MathF.Round(MathF.Abs(uv.X) + 0.5f) :
163+
// uv.X > 1 ? uv.X % 1f :
164+
// uv.X,
165+
// uv.Y < -1 ? -(MathF.Abs(uv.Y) % 1f) + MathF.Round(MathF.Abs(-(MathF.Abs(uv.Y) % 1f)) + 0.5f) :
166+
// uv.Y < 0 ? uv.Y + MathF.Round(MathF.Abs(uv.Y) + 0.5f) :
167+
// uv.Y > 1 ? uv.Y % 1f :
168+
// uv.Y
169+
//);
170+
171+
172+
if (uv.X < -1)
173+
uv.X = -(MathF.Abs(uv.X) % 1f);
174+
if (uv.Y < -1)
175+
uv.Y = -(MathF.Abs(uv.Y) % 1f);
176+
177+
if (uv.X < 0)
178+
uv.X = (uv.X + 100);
179+
if (uv.Y < 0)
180+
uv.Y = (uv.Y + 100);
181+
182+
if (uv.X > 1)
183+
uv.X = uv.X % 1.0f;
184+
if (uv.Y > 1)
185+
uv.Y = uv.Y % 1.0f;
186+
187+
return new Point(uv.X, uv.Y);
159188
}
160189

161190
private static BitmapSource GetBitMap(SixLabors.ImageSharp.Image<Rgba32> image)

RenderWarePreviewer/Scenes/SceneManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using SixLabors.ImageSharp.PixelFormats;
66
using System;
77
using System.Collections.Generic;
8+
using System.IO;
89
using System.Linq;
910
using System.Numerics;
1011
using System.Windows.Controls;
@@ -134,6 +135,16 @@ public Image<Rgba32> GetImage(GtaModel gtaModel, string texture)
134135
return image;
135136
}
136137

138+
public byte[] GetDff(GtaModel gtaModel)
139+
{
140+
return this.assetHelper.GetRawDff(gtaModel.ModelName);
141+
}
142+
143+
public byte[] GetTxd(GtaModel gtaModel)
144+
{
145+
return this.assetHelper.GetRawTxd(gtaModel.TxdName);
146+
}
147+
137148
public void ApplyTo(Viewport3D viewport)
138149
{
139150
this.scene.ApplyTo(viewport);

RenderWarePreviewer/Ui/Controls/ModelDetail.xaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@
77
mc:Ignorable="d"
88
d:DesignHeight="450" d:DesignWidth="200">
99
<StackPanel>
10+
<StackPanel Name="ExportPanel" Margin="0, 20, 0, 0" Orientation="Horizontal" HorizontalAlignment="Right">
11+
<Button Margin="5, 5, 5, 0" Width="100" HorizontalAlignment="Right" Click="ExportDff" BorderBrush="#34495E" Background="#34495E" Foreground="White" Height="30">Export DFF
12+
<Button.Resources>
13+
<Style TargetType="Border">
14+
<Setter Property="CornerRadius" Value="5"/>
15+
</Style>
16+
</Button.Resources>
17+
</Button>
18+
<Button Margin="5, 5, 5, 0" Width="100" HorizontalAlignment="Right" Click="ExportTxd" BorderBrush="#34495E" Background="#34495E" Foreground="White" Height="30">Export TXD
19+
<Button.Resources>
20+
<Style TargetType="Border">
21+
<Setter Property="CornerRadius" Value="5"/>
22+
</Style>
23+
</Button.Resources>
24+
</Button>
25+
</StackPanel>
1026
<StackPanel Name="TextureStackPanel" Margin="0, 20, 0, 0">
1127
<Label Foreground="white">Texture:</Label>
1228
<ComboBox Name="TextureComboBox" Margin="5, 0, 5, 0" SelectionChanged="SelectTexture">
1329
<ComboBoxItem>Texture #1</ComboBoxItem>
1430
</ComboBox>
15-
<Button Margin="5, 5, 5, 0" Width="74" HorizontalAlignment="Right" Click="ExportImage" BorderBrush="#34495E" Background="#34495E" Foreground="White" Height="30">Export
31+
<Button Margin="5, 5, 5, 0" Width="100" HorizontalAlignment="Right" Click="ExportImage" BorderBrush="#34495E" Background="#34495E" Foreground="White" Height="30">Export Texture
1632
<Button.Resources>
1733
<Style TargetType="Border">
1834
<Setter Property="CornerRadius" Value="5"/>
@@ -26,7 +42,7 @@
2642
<AccessText Name="ImagePathLabel" TextWrapping="Wrap"/>
2743
</Label.Content>
2844
</Label>
29-
<Button BorderBrush="#34495E" Background="#34495E" Foreground="White" Height="30" Click="SelectTargetFile">Select Image File
45+
<Button BorderBrush="#34495E" Background="#34495E" Foreground="White" Height="30" Click="SelectTargetFile">Select Image File For Live Reload
3046
<Button.Resources>
3147
<Style TargetType="Border">
3248
<Setter Property="CornerRadius" Value="5"/>

RenderWarePreviewer/Ui/Controls/ModelDetail.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public void ExportImage(object sender, RoutedEventArgs e)
9696
var model = this.loadedModel;
9797
var texture = this.SelectedTexture;
9898

99+
if (model == null)
100+
return;
101+
99102
var dialog = new System.Windows.Forms.SaveFileDialog()
100103
{
101104
FileName = texture + ".png"
@@ -108,6 +111,44 @@ public void ExportImage(object sender, RoutedEventArgs e)
108111
image.SaveAsPng(dialog.FileName);
109112
}
110113

114+
public void ExportDff(object sender, RoutedEventArgs e)
115+
{
116+
var model = this.loadedModel;
117+
118+
if (model == null || this.sceneManager == null)
119+
return;
120+
121+
var dialog = new System.Windows.Forms.SaveFileDialog()
122+
{
123+
FileName = model.ModelName + ".dff"
124+
};
125+
126+
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
127+
return;
128+
129+
var dff = this.SceneManager.GetDff(model);
130+
File.WriteAllBytes(dialog.FileName, dff);
131+
}
132+
133+
public void ExportTxd(object sender, RoutedEventArgs e)
134+
{
135+
var model = this.loadedModel;
136+
137+
if (model == null || this.sceneManager == null)
138+
return;
139+
140+
var dialog = new System.Windows.Forms.SaveFileDialog()
141+
{
142+
FileName = model.TxdName + ".txd"
143+
};
144+
145+
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
146+
return;
147+
148+
var txd = this.SceneManager.GetTxd(model);
149+
File.WriteAllBytes(dialog.FileName, txd);
150+
}
151+
111152
public void SelectTargetFile(object sender, RoutedEventArgs e)
112153
{
113154
if (this.watcher != null)

0 commit comments

Comments
 (0)