[gnome-games/wip/exalm/rebrand: 113/124] retro-firmware: Merge into Firmwar




commit 30931917b63c4fb9524e6df75e106dbeab9b4736
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Mar 31 00:54:57 2021 +0500

    retro-firmware: Merge into Firmwar

 src/core/core.vala            |  2 +-
 src/core/firmware.vala        | 56 +++++++++++++++++++++++++++++++++++++++---
 src/meson.build               |  1 -
 src/retro/retro-firmware.vala | 57 -------------------------------------------
 4 files changed, 54 insertions(+), 62 deletions(-)
---
diff --git a/src/core/core.vala b/src/core/core.vala
index a2b6acb6..5a6c308d 100644
--- a/src/core/core.vala
+++ b/src/core/core.vala
@@ -12,7 +12,7 @@ public class Games.Core : Object {
                Firmware[] firmware_list = {};
                if (core_descriptor.has_firmwares (platform_id))
                        foreach (var firmware in core_descriptor.get_firmwares (platform_id))
-                               firmware_list += new RetroFirmware (firmware, core_descriptor);
+                               firmware_list += new Firmware (firmware, core_descriptor);
 
                return firmware_list;
        }
diff --git a/src/core/firmware.vala b/src/core/firmware.vala
index 800970d7..14321d9f 100644
--- a/src/core/firmware.vala
+++ b/src/core/firmware.vala
@@ -1,7 +1,57 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
-public interface Games.Firmware : Object {
-       public abstract bool get_is_mandatory ();
+public class Games.Firmware : Object {
+       private string name;
+       private bool is_mandatory;
+       private string path;
+       private string? md5;
+       private string? sha512;
 
-       public abstract void check_is_valid (Platform platform) throws FirmwareError;
+       public Firmware (string name, Retro.CoreDescriptor core_descriptor) throws Error {
+               this.name = name;
+
+               if (core_descriptor.has_firmware_md5 (name))
+                       md5 = core_descriptor.get_firmware_md5 (name);
+               if (core_descriptor.has_firmware_sha512 (name))
+                       sha512 = core_descriptor.get_firmware_sha512 (name);
+
+               is_mandatory = core_descriptor.get_is_firmware_mandatory (name);
+               path = core_descriptor.get_firmware_path (name);
+       }
+
+       public bool get_is_mandatory () {
+               return is_mandatory;
+       }
+
+       public void check_is_valid (Platform platform) throws FirmwareError {
+               var firmware_dir = File.new_for_path (platform.get_system_dir ());
+
+               var firmware = firmware_dir.get_child (path);
+               if (!firmware.query_exists ())
+                       throw new FirmwareError.FIRMWARE_NOT_FOUND ("This game requires the %s firmware file 
to run.", firmware.get_path ());
+
+               if (md5 == null && sha512 == null)
+                       return;
+
+               try {
+                       var stream = firmware.read ();
+
+                       stream.seek (0, SeekType.END);
+                       var size = (size_t) stream.tell ();
+                       stream.seek (0, SeekType.SET);
+                       var bytes = stream.read_bytes (size);
+
+                       if (md5 != null) {
+                               if (Checksum.compute_for_bytes (ChecksumType.MD5, bytes) != md5)
+                                       throw new FirmwareError.FIRMWARE_NOT_FOUND ("This game requires the 
%s firmware file with a MD5 fingerprint of %s to run.", firmware.get_path (), md5);
+                       }
+
+                       if (sha512 != null) {
+                               if (Checksum.compute_for_bytes (ChecksumType.SHA512, bytes) != sha512)
+                                       throw new FirmwareError.FIRMWARE_NOT_FOUND ("This game requires the 
%s firmware file with a SHA-512 fingerprint of %s to run.", firmware.get_path (), sha512);
+                       }
+               } catch (Error e) {
+                       throw new FirmwareError.FIRMWARE_NOT_FOUND (e.message);
+               }
+       }
 }
diff --git a/src/meson.build b/src/meson.build
index fc5b2c00..a3f345ff 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -141,7 +141,6 @@ vala_sources = [
   'retro/retro-core-manager.vala',
   'retro/retro-core-source.vala',
   'retro/retro-error.vala',
-  'retro/retro-firmware.vala',
   'retro/retro-gamepad.vala',
   'retro/retro-input-manager.vala',
   'retro/retro-options.vala',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]