Tweaks relating to previous commit

This commit is contained in:
Maruno17
2021-02-25 22:57:39 +00:00
parent 1f2309c4d2
commit 46ed10a31d
9 changed files with 31 additions and 146 deletions

View File

@@ -38,113 +38,6 @@ end
#####################################################################
# TODO: Delete this class in Ruby 2+.
class WeakRef
@@id_map = {}
@@id_rev_map = {}
@@final = lambda do |id|
rids = @@id_map[id]
if rids
rids.each { |rid| @@id_rev_map.delete(rid) }
@@id_map.delete(id)
end
rid = @@id_rev_map[id]
if rid
@@id_rev_map.delete(id)
@@id_map[rid].delete(id)
@@id_map.delete(rid) if @@id_map[rid].empty?
end
end
# Create a new WeakRef from +orig+.
def initialize(orig)
__setobj__(orig)
end
def __getobj__
unless @@id_rev_map[self.__id__] == @__id
return nil
end
begin
ObjectSpace._id2ref(@__id)
rescue RangeError
return nil
end
end
def __setobj__(obj)
@__id = obj.__id__
unless @@id_rev_map.key?(self)
ObjectSpace.define_finalizer obj, @@final
ObjectSpace.define_finalizer self, @@final
end
@@id_map[@__id] = [] unless @@id_map[@__id]
@@id_map[@__id].push self.__id__
@@id_rev_map[self.__id__] = @__id
end
# Returns true if the referenced object still exists, and false if it has
# been garbage collected.
def weakref_alive?
@@id_rev_map[self.__id__] == @__id
end
end
# TODO: Delete this class in Ruby 2+.
class WeakHashtable
include Enumerable
def initialize
@hash = {}
end
def clear
@hash.clear
end
def delete(value)
@hash.delete(value)
end
def include?(value)
@hash.include?(value)
end
def each
@hash.each { |i| yield i }
end
def keys
@hash.keys
end
def values
@hash.values
end
def [](key)
o = @hash[key]
return o if !o
if o.weakref_alive?
o = o.__getobj__
else
@hash.delete(key)
o = nil
end
return o
end
def []=(key, o)
o = WeakRef.new(o) if o != nil
@hash[key] = o
end
end
module RPG
module Cache
def self.load_bitmap(folder_name, filename, hue = 0)
@@ -247,9 +140,7 @@ end
module BitmapCache
# TODO: Replace this with the commented line in Ruby 2+.
@cache = WeakHashtable.new
# @cache = ObjectSpace::WeakMap.new
@cache = ObjectSpace::WeakMap.new
def self.fromCache(i)
return nil if !@cache.include?(i)
@@ -279,17 +170,9 @@ module BitmapCache
def self.load_bitmap(path, hue = 0, failsafe = false)
cached = true
path = canonicalize(path)
path = -canonicalize(path) # Creates a frozen string from the path, to ensure identical paths are treated as identical
objPath = fromCache(path)
if !objPath
# TODO: Delete this in Ruby 2+.
@cleancounter = ((@cleancounter || 0) + 1) % 10
if @cleancounter == 0
for i in @cache.keys
@cache.delete(i) if !fromCache(i)
end
end
# TODO: Up to here.
begin
bm = BitmapWrapper.new(path)
rescue Hangup
@@ -402,9 +285,7 @@ module BitmapCache
end
def self.clear
# TODO: Replace this with the commented line in Ruby 2+.
@cache.clear
# @cache = ObjectSpace::WeakMap.new
@cache = ObjectSpace::WeakMap.new
GC.start
end
end