From 632bcccef3095a73a7c361a700dc18c70ec6648f Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 24 Mar 2021 14:21:46 +0530 Subject: [PATCH] Add monix-nio and make modding sys use it --- build.sbt | 41 ++++++++++--------- .../moddingsystem/ModdingSystem.scala | 26 +++++++----- .../scala/wow/doge/mygame/utils/package.scala | 32 +++++++++++++++ 3 files changed, 69 insertions(+), 30 deletions(-) mode change 100644 => 100755 build.sbt mode change 100644 => 100755 src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala mode change 100644 => 100755 src/main/scala/wow/doge/mygame/utils/package.scala diff --git a/build.sbt b/build.sbt old mode 100644 new mode 100755 index 1f3d5fb..9ac3e53 --- a/build.sbt +++ b/build.sbt @@ -42,6 +42,7 @@ lazy val root = (project in file(".")).settings( "org.typelevel" %% "cats-effect" % "2.3.0", "io.monix" %% "monix" % "3.2.2", "io.monix" %% "monix-bio" % "1.1.0", + "io.monix" %% "monix-nio" % "0.0.9", "io.circe" %% "circe-core" % "0.13.0", "io.circe" %% "circe-generic" % "0.13.0", "com.softwaremill.sttp.client3" %% "core" % "3.0.0", @@ -143,7 +144,7 @@ inThisBuild( List( scalaVersion := scalaVersion.value, // 2.11.12, or 2.13.3 semanticdbEnabled := true, // enable SemanticDB - semanticdbVersion := "4.3.24" // use Scalafix compatible version + semanticdbVersion := "4.4.10" // use Scalafix compatible version ) ) ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.4.3" @@ -157,23 +158,23 @@ scalafixDependencies in ThisBuild += "org.scalalint" %% "rules" % "0.1.4" // wartremoverWarnings in (Compile, compile) ++= Seq(Wart.Any, Wart.Serializable) -wartremoverErrors in (Compile, compile) ++= - Warts.allBut( - Wart.Any, - Wart.NonUnitStatements, - // Wart.StringPlusAny, - Wart.Overloading, - Wart.PublicInference, - Wart.Nothing, - Wart.Var, - Wart.DefaultArguments, - // Wart.MutableDataStructures, - Wart.ImplicitConversion, - Wart.ImplicitParameter, - Wart.ToString, - Wart.Recursion, - Wart.While, - Wart.ExplicitImplicitTypes, - Wart.ListUnapply - ) +// wartremoverErrors in (Compile, compile) ++= +// Warts.allBut( +// Wart.Any, +// Wart.NonUnitStatements, +// // Wart.StringPlusAny, +// Wart.Overloading, +// Wart.PublicInference, +// Wart.Nothing, +// Wart.Var, +// Wart.DefaultArguments, +// // Wart.MutableDataStructures, +// Wart.ImplicitConversion, +// Wart.ImplicitParameter, +// Wart.ToString, +// Wart.Recursion, +// Wart.While, +// Wart.ExplicitImplicitTypes, +// Wart.ListUnapply +// ) // Seq(Wart.FinalCaseClass) diff --git a/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala b/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala old mode 100644 new mode 100755 index b6cf738..dbf6278 --- a/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala +++ b/src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala @@ -16,9 +16,8 @@ import monix.bio.IO import monix.bio.UIO import monix.reactive.Consumer import monix.reactive.Observable -import wow.doge.mygame.utils.IOUtils - -import IOUtils.toIO +import wow.doge.mygame.implicits._ +import wow.doge.mygame.utils.readAsyncL @JsonCodec final case class Test1(hello1: String, hello2: String) @@ -43,13 +42,20 @@ object ModdingSystem { implicit val eq = Eq.fromUniversalEquals[Error] } + def readPluginsList2(dir: os.Path) = + for { + contents <- readAsyncL(dir) + } yield () + def readPluginsList(dir: os.Path): IO[Error, ArraySeq[Plugin]] = - IO(os.read(dir / "plugins.json")) + // IO(os.read(dir / "plugins.json")) + readAsyncL(dir / "plugins.json") .onErrorHandleWith { case _: FileNotFoundException => IO.raiseError(FileNotFound(dir / "plugins.json")) case _: NoSuchFileException => IO.raiseError(FileNotFound(dir / "plugins.json")) + case other => IO.terminate(other) } .flatMap(files => IO.fromEither(parse(files)) @@ -197,12 +203,12 @@ object ModdingSystem { UIO(readSuccesses.to(List)), UIO(parseFailures.to(List)), UIO(parseSuccesses.to(List)), - toIO( - Observable - .fromIterable(parseSuccesses) - .map { case (p, json) => json } - .consumeWith(loadBalancedPluginDataMerger) - ).hideErrors + Observable + .fromIterable(parseSuccesses) + .map { case (p, json) => json } + .consumeWith(loadBalancedPluginDataMerger) + .toIO + .hideErrors )(Result.apply) } yield res diff --git a/src/main/scala/wow/doge/mygame/utils/package.scala b/src/main/scala/wow/doge/mygame/utils/package.scala old mode 100644 new mode 100755 index 29d2714..bf02cb6 --- a/src/main/scala/wow/doge/mygame/utils/package.scala +++ b/src/main/scala/wow/doge/mygame/utils/package.scala @@ -1,6 +1,38 @@ package wow.doge.mygame +import java.nio.file.StandardOpenOption + +import monix.bio.UIO +import monix.execution.Scheduler +import monix.nio.file.AsyncFileChannelConsumer +import monix.nio.text.UTF8Codec.utf8Decode +import monix.nio.{file => mnf} +import wow.doge.mygame.implicits._ + package object utils { def methodName(implicit enclosing: sourcecode.Enclosing) = enclosing.value.split(" ")(0).split("""\.""").last + + def readAsync( + path: os.Path, + chunkSize: Int = 8192 + ) = + UIO + .deferAction(implicit s => UIO(mnf.readAsync(path.toNIO, chunkSize))) + .flatMap(bytes => UIO(bytes.pipeThrough(utf8Decode))) + + @SuppressWarnings(Array("org.wartremover.warts.TraversableOps")) + def readAsyncL( + path: os.Path, + chunkSize: Int = 8192 + ) = + readAsync(path, chunkSize) + .flatMap(_.toListL.toIO) + .map(_.head) + + def writeAsync(path: os.Path, flags: Seq[StandardOpenOption] = Seq.empty)( + implicit s: Scheduler + ): AsyncFileChannelConsumer = + mnf.appendAsync(path.toNIO, 0, flags) + }