Skip to content
This repository was archived by the owner on Oct 29, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 75 additions & 18 deletions lib/features/create/create_finish/create_finish_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,16 @@ Future<Tuple2<String, Uint8List?>> processTextureEntry(
final pair = params.item2;
final baseDir = params.item1;
final isAlt = params.item3;

final textureName = dartp.basenameWithoutExtension(pair.item1.path);
final fileName = dartp.join(baseDir, textureName);
final targetPath = pair.item2.targetName;
final textureName = targetPath != null
? dartp.basename(targetPath)
: dartp.basenameWithoutExtension(pair.item1.path);
final fileName = targetPath ?? dartp.join(baseDir, textureName);

final data = await (pair.item2.textureType == TextureType.JPEG32bpp
? processJPEG
: processPNG)(pair, textureName);
final finalPath = isAlt ? dartp.join('alt', fileName) : fileName;
final finalPath = p.normalize(isAlt ? dartp.join('alt', fileName) : fileName);
return Tuple2(finalPath, data);
}

Expand Down Expand Up @@ -317,45 +319,100 @@ Future<Uint8List?> processPNG(
String textureName,
) async {
final texture = Texture.empty();
final entry = pair.item2;
final imageData = await pair.item1.readAsBytes();
final image = decodePng(imageData);
var image = decodePng(imageData);

if (image == null) {
log('Failed to decode image data for PNG: $textureName');
return null;
}

texture.textureType = pair.item2.textureType;
if (entry.kind == TextureEntryKind.additiveFontGlyph) {
var k = exactMultiple(image, entry.textureWidth, entry.textureHeight);
if (k == null && entry.tileWidth != null && entry.tileHeight != null) {
k = exactMultiple(image, entry.tileWidth!, entry.tileHeight!);
}
if (k == null) {
log('Skipping $textureName: ${image.width}x${image.height} is not an '
'integer multiple of its ${entry.textureWidth}x${entry.textureHeight}'
' mask chunk or of its drawn tile');
return null;
}
final alignedWidth = (entry.textureWidth + 3) & ~3;
image = padCanvas(image, k * alignedWidth, k * entry.textureHeight);
texture.textureType = TextureType.RGBA32bpp;
texture.setTextureFlags(LOAD_AS_RAW);
texture.setTextureScale(k.toDouble(), k.toDouble());
texture.fromRawImage(image);
return texture.build();
}

if (entry.tileWidth != null &&
entry.tileHeight != null &&
exactMultiple(image, entry.textureWidth, entry.textureHeight) == null) {
final k = exactMultiple(image, entry.tileWidth!, entry.tileHeight!);
if (k != null) {
image = padCanvas(
image, k * entry.textureWidth, k * entry.textureHeight,);
}
}

texture.textureType = entry.textureType;
texture.isPalette = image.hasPalette && (texture.textureType == TextureType.Palette4bpp || texture.textureType == TextureType.Palette8bpp);

final isNotOriginalSize = pair.item2.textureWidth != image.width ||
pair.item2.textureHeight != image.height;
if (isNotOriginalSize) {
final isNotOriginalSize = entry.textureWidth != image.width ||
entry.textureHeight != image.height;
final isAdditive = entry.kind == TextureEntryKind.additive;
if (isAdditive) {
texture.isPalette = false;
}
if (isNotOriginalSize || isAdditive) {
texture.setTextureFlags(LOAD_AS_RAW);
if (!image.hasPalette || !texture.isPalette) {
texture.textureType = TextureType.RGBA32bpp;
}

final hByteScale = (image.width / pair.item2.textureWidth) *
final hByteScale = (image.width / entry.textureWidth) *
(texture.textureType.pixelMultiplier /
pair.item2.textureType.pixelMultiplier);
final vPixelScale = image.height / pair.item2.textureHeight;
entry.textureType.pixelMultiplier);
final vPixelScale = image.height / entry.textureHeight;
texture.setTextureScale(hByteScale, vPixelScale);
}

texture.fromRawImage(image);

if (pair.item2.textureType == TextureType.Palette8bpp ||
pair.item2.textureType == TextureType.Palette4bpp) {
if (entry.textureType == TextureType.Palette8bpp ||
entry.textureType == TextureType.Palette4bpp) {
if (texture.isPalette) {
texture.textureType = pair.item2.textureType;
} else if (!isNotOriginalSize) {
texture.textureType = entry.textureType;
} else if (!isNotOriginalSize && !isAdditive) {
print('Skipping $textureName because it is not a palette texture');
return null;
}
} else {
texture.textureType = pair.item2.textureType;
} else if (!isAdditive) {
texture.textureType = entry.textureType;
}

return texture.build();
}

int? exactMultiple(Image image, int width, int height) {
if (width <= 0 || height <= 0) {
return null;
}
if (image.width % width != 0 || image.height % height != 0) {
return null;
}
final k = image.width ~/ width;
return (k > 0 && image.height ~/ height == k) ? k : null;
}

Image padCanvas(Image image, int width, int height) {
if (image.width == width && image.height == height) {
return image;
}
final canvas = Image(width: width, height: height, numChannels: 4);
compositeImage(canvas, image, dstX: 0, dstY: 0, blend: BlendMode.direct);
return canvas;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'package:flutter/services.dart';
import 'package:image/image.dart';
import 'package:path/path.dart' as path;
import 'package:retro/arc/arc.dart';
import 'package:retro/games/bk64/bk64_conventions.dart';
import 'package:retro/games/game_texture_conventions.dart';
import 'package:retro/models/texture_manifest_entry.dart';
import 'package:retro/otr/resource.dart';
import 'package:retro/otr/resource_type.dart';
Expand Down Expand Up @@ -168,6 +170,26 @@ Future<HashMap<String, ProcessedFilesInFolder>?> processFolder(
final manifestContents = await manifestFile.readAsString();
final manifest = json.decode(manifestContents) as Map<String, dynamic>;

// Optional aliases.json: maps source image paths (relative to the folder,
// extension included) onto archive texture paths, so externally-named packs
// can be repacked without renaming thousands of files.
final aliases = <String, List<String>>{};
final aliasFile = File(p.normalize('$folderPath/aliases.json'));
if (aliasFile.existsSync()) {
final aliasJson =
json.decode(await aliasFile.readAsString()) as Map<String, dynamic>;
for (final alias in aliasJson.entries) {
final targets = alias.value is List
? (alias.value as List).cast<String>()
: <String>[alias.value as String];
aliases[p.normalize(alias.key)] = targets
.map((target) => p.normalize(
target.startsWith('alt/') ? target.substring(4) : target,),)
.toList();
}
log('Loaded aliases.json with ${aliases.length} source images');
}

// find all images in folder
final supportedExtensions = <String>['.png', '.jpeg', '.jpg'];
final files = Directory(folderPath).listSync(recursive: true);
Expand All @@ -177,34 +199,65 @@ Future<HashMap<String, ProcessedFilesInFolder>?> processFolder(

for (final rawFile in texFiles) {
final texFile = File(p.normalize(rawFile.path));
final relativePath = path.relative(texFile.path, from: folderPath);
final relativePath = p.normalize(path.relative(texFile.path, from: folderPath));
final texPathRelativeToFolder = p.normalize(path.withoutExtension(relativePath));
final targets = aliases[relativePath] ?? [texPathRelativeToFolder];

if (manifest.containsKey(texPathRelativeToFolder)) {
final manifestEntry = TextureManifestEntry.fromJson(
manifest[texPathRelativeToFolder] as Map<String, dynamic>);
for (final target in targets) {
final manifestEntry = resolveTextureEntry(manifest, target);
if (manifestEntry == null) {
log('Found file not present in manifest: $target');
continue;
}

final texFileBytes = await texFile.readAsBytes();
final texFileHash = sha256.convert(texFileBytes).toString();
if (manifestEntry.kind == TextureEntryKind.replacement) {
final texFileBytes = await texFile.readAsBytes();
final texFileHash = sha256.convert(texFileBytes).toString();
if (manifestEntry.hash == texFileHash) {
continue;
}
log('Found file with changed hash: $target');
} else {
log('Staging additive texture: $target');
}

if (manifestEntry.hash != texFileHash) {
log('Found file with changed hash: $texPathRelativeToFolder');
final pathWithoutFilename = path.dirname(texPathRelativeToFolder);
manifestEntry.targetName = target;
final pathWithoutFilename = path.dirname(target);

if (processedFiles.containsKey(pathWithoutFilename)) {
processedFiles[pathWithoutFilename]!.add(Tuple2(texFile, manifestEntry));
} else {
processedFiles[pathWithoutFilename] = [Tuple2(texFile, manifestEntry)];
}
if (processedFiles.containsKey(pathWithoutFilename)) {
processedFiles[pathWithoutFilename]!.add(Tuple2(texFile, manifestEntry));
} else {
processedFiles[pathWithoutFilename] = [Tuple2(texFile, manifestEntry)];
}
} else {
log('Found file not present in manifest: $texPathRelativeToFolder');
}
}

return processedFiles;
}

// Game ports whose extra texture conventions the shared flow consults
const List<GameTextureConventions> gameTextureConventions = [
Bk64TextureConventions(),
];

// Look a target path up in the manifest; on a miss, let each game's
// conventions try to resolve it as an additive path with a template
// entry describing how to convert it.
TextureManifestEntry? resolveTextureEntry(
Map<String, dynamic> manifest, String target) {
if (manifest.containsKey(target)) {
return TextureManifestEntry.fromJson(
manifest[target] as Map<String, dynamic>);
}
for (final game in gameTextureConventions) {
final entry = game.resolveAdditiveEntry(manifest, target);
if (entry != null) {
return entry;
}
}
return null;
}

Future<HashMap<String, TextureManifestEntry>?> processOTR(
Tuple2<List<String>, String> params) async {
final processedFiles = HashMap<String, TextureManifestEntry>();
Expand Down Expand Up @@ -232,6 +285,10 @@ Future<HashMap<String, TextureManifestEntry>?> processOTR(
arcFile.close();
}

for (final game in gameTextureConventions) {
await game.recordExtractionMetadata(params.item1, processedFiles);
}

return processedFiles.isEmpty ? null : processedFiles;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:retro/l10n/app_localizations.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:retro/ui/components/custom_scaffold.dart';
import 'package:retro/ui/components/option_card.dart';

Expand Down Expand Up @@ -31,14 +30,14 @@ class _Ship2GameScreenState extends State<Ship2GameScreen> {
children: [
OptionCard(
text: i18n.gameSelectionScreenSoh_title,
icon: FontAwesomeIcons.puzzlePiece,
icon: Icons.extension,
onTap: () {
Navigator.of(context).pushNamed('/game_selection/soh');
},
),
OptionCard(
text: i18n.gameSelectionScreen2Ship_title,
icon: FontAwesomeIcons.drum,
icon: Icons.music_note,
onTap: () {
Navigator.of(context).pushNamed('/create_replace_textures');
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:retro/l10n/app_localizations.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:retro/ui/components/custom_scaffold.dart';
import 'package:retro/ui/components/option_card.dart';

Expand Down Expand Up @@ -38,7 +37,7 @@ class _SOHGameScreenState extends State<SOHGameScreen> {
),
OptionCard(
text: i18n.sohCreateDebugFontScreen_title,
icon: FontAwesomeIcons.font,
icon: Icons.text_fields,
onTap: () {
Navigator.of(context).pushNamed('/debug_generate_font');
},
Expand Down
Loading
Loading