Skip to content

Commit

Permalink
DCP, DIM: Fix image import, broken in recent releases
Browse files Browse the repository at this point in the history
  • Loading branch information
keirf committed Nov 23, 2023
1 parent e5c8413 commit 5ad2981
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
20 changes: 11 additions & 9 deletions src/greaseweazle/image/dcp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# greaseweazle/image/dim.py
# greaseweazle/image/dcp.py
#
# Written & released by Keir Fraser <[email protected]>
#
Expand All @@ -8,24 +8,27 @@
import struct

from greaseweazle import error
from greaseweazle.image.img import IMG
from greaseweazle.image.img import IMG_AutoFormat
from greaseweazle.codec import codec
from .image import Image

class DCP(IMG):
class DCP(IMG_AutoFormat):

read_only = True

@staticmethod
def format_from_file(name: str) -> codec.DiskDef:
fmt = codec.get_diskdef('pc98.2hd')
assert fmt is not None # mypy
return fmt

def from_bytes(self, dat: bytes) -> None:

header = dat[:162]
pos = 162
format_str = 'pc98.2hd'
fmt = codec.get_diskdef(format_str)
assert fmt is not None # mypy
fmt = self.fmt

for t in self.track_list():
cyl, head = t.cyl, t.head
for cyl, head in self.track_list():
if self.sides_swapped:
head ^= 1
track = fmt.mk_track(cyl, head)
Expand All @@ -38,7 +41,6 @@ def from_bytes(self, dat: bytes) -> None:
self.to_track[cyl,head] = track
elif header[cyl * 2 + head] != 0:
raise error.Fatal("DCP: Corrupt header.")
self.format_str = format_str

# Local variables:
# python-indent: 4
Expand Down
20 changes: 14 additions & 6 deletions src/greaseweazle/image/dim.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
import struct

from greaseweazle import error
from greaseweazle.image.img import IMG
from greaseweazle.image.img import IMG_AutoFormat
from greaseweazle.codec import codec
from .image import Image

class DIM(IMG):
default_format = None
class DIM(IMG_AutoFormat):

read_only = True

def from_bytes(self, dat: bytes) -> None:
@staticmethod
def format_from_file(name: str) -> codec.DiskDef:

with open(name, "rb") as f:
header = f.read(256)

header = dat[:256]
error.check(header[0xAB:0xB8] == b"DIFC HEADER ",
"DIM: Not a DIM file.")
media_byte, = struct.unpack('B255x', header)
Expand All @@ -28,10 +31,16 @@ def from_bytes(self, dat: bytes) -> None:
format_str = 'pc98.2hs'
else:
raise error.Fatal("DIM: Unsupported format.")

fmt = codec.get_diskdef(format_str)
assert fmt is not None # mypy
return fmt

def from_bytes(self, dat: bytes) -> None:

pos = 256
fmt = self.fmt

for t in fmt.tracks:
cyl, head = t.cyl, t.head
if self.sides_swapped:
Expand All @@ -40,7 +49,6 @@ def from_bytes(self, dat: bytes) -> None:
if track is not None:
pos += track.set_img_track(dat[pos:])
self.to_track[cyl,head] = track
self.format_str = format_str

# Local variables:
# python-indent: 4
Expand Down
16 changes: 15 additions & 1 deletion src/greaseweazle/image/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from greaseweazle import error
from greaseweazle.codec import codec
from greaseweazle.flux import HasFlux
from .image import Image
from .image import Image, OptDict

class IMG(Image):

Expand Down Expand Up @@ -91,6 +91,20 @@ def get_image(self) -> bytes:
return tdat


class IMG_AutoFormat(IMG):

@staticmethod
def format_from_file(name: str) -> codec.DiskDef:
raise NotImplementedError

@classmethod
def from_file(cls, name: str, fmt: Optional[codec.DiskDef],
opts: OptDict) -> Image:
error.check(fmt is None,
f'{cls.__name__}: Format cannot be overridden')
fmt = cls.format_from_file(name)
return super().from_file(name, fmt, opts)

# Local variables:
# python-indent: 4
# End:
4 changes: 0 additions & 4 deletions src/greaseweazle/tools/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,6 @@ def main(argv) -> None:
print("Writing " + str(args.tracks))
if args.precomp is not None:
print(args.precomp)
if hasattr(image, 'format_str'):
print("Image format " + image.format_str)
error.check(args.format is None,
'Cannot override image format with --format')
if args.format:
print("Format " + args.format)
try:
Expand Down

0 comments on commit 5ad2981

Please sign in to comment.